[Java] Object-oriented summary_Part 2

Last time

[Java] Object-Oriented Summary \ _Part 1 -Qiita

Effect

Last time, it was about class creation and instantiation.

This time, we will perform instance operation and finish once.

Partial correction

Objects in programming Before correction: "object"

Edited: "Gathering of information" Reason: Because there are inconveniences in "objects" in the real world.

Example: Is air an "object"? → Yes

Is air an "object"? → No Is air a "collection of information"? → Yes

Is the schedule an "object"? → Yes

Is the schedule an "object"? → No Is the schedule "a gathering of information"? → Yes

reference

[Java] What is an object? \ | The easiest introduction to Java Introduction to Java Object Orientation

[Java] What is a constructor? Meaning of this \ () \ | The easiest introduction to Java

Ready to use the instance

Previous code (repost)

Car.java


class Car {
	String carname = "A"; //Car name
	String color = "red"; //Car color

	void start() { //Car outgoing operation
		System.out.println("Take off");
	}
 
	void stop() { //Car stop operation
		System.out.println("Stop");
	}
}

[Program description]

--Creating a Car class

CarTest.java


class CarTest{
	public static void main(String[] args){
		Car car = new Car();
	}
}

[Program description]

--Substitute a Car instance for a Car-type reference variable car --The reference (position of the instance body) is assigned instead of the instance body. 参照.png

Manipulate instance fields through reference variables

CarTest.java


class CarTest{
	public static void main(String[] args){
		Car car;
		car = new Car();
		car.carname = "B"; //The name of the car"B"change to
		car.color = "blue"; //The color of the car"blue"change to
	}
}

[Program description]

--Substitute B for the car name. --Substitute blue for car color. --If you use reference type variable name.field name, you can operate the instance field.

参照(フィールド操作).png

Output instance fields through reference variables

CarTest.java


class CarTest{
	public static void main(String[] args){
		Car car;
		car = new Car();
		car.carname = "B";
		car.color = "blue";
		System.out.println(car.carname); //Output result:B
		System.out.println(car.color); //Output result:blue
	}
}

[Program description]

--The output is basically the same, and can be operated as a reference variable name.field name.

Execute a method of an instance through a reference type variable

CarTest.java


class CarTest{
	public static void main(String[] args){
		Car car;
		car = new Car();
		car.carname = "B";
		car.color = "blue";
		car.start(); //Output result:I will send
		car.stop(); //Output result:Stop
	}
}

[Program description]

--The method is the same as the field, and can be operated as a reference variable name.method name.

参照(メソッド操作).png

result

スクリーンショット 2019-04-28 0.33.34.png

Summary

Overview:

--An object in programming is a "collection of information" with a "name". There is an entity. --Object-oriented is the way of thinking for handling objects. --Class is a design document. Instance is an entity created by class type.

About instance operation:

--To operate an instance, follow the procedure "Create class-> Instantiate-> Save instance reference-> Operate instance fields and methods through reference".

Others:

--Through terms other than instance operations that seem difficult here. --Constructor / Default constructor / this / this () / Encapsulation / Inheritance / Polymorphism / Garbage collector

Recommended Posts

[Java] Object-oriented summary_Part 1
[Java] Object-oriented summary_Part 2
[Java] Object-oriented
Object-oriented FizzBuzz (Java)
[Java] Object-oriented syntax-Constructor
Object-oriented (Java) basics
[Java] Object-oriented syntax-Package
Object-oriented (java) with Strike Gundam
Object-oriented summary by beginners (Java)
Java
Java 3 major elements (object-oriented) memorandum
Java
[Java] Object-oriented syntax --class method / argument
[Java] Object-oriented syntax-class / field / method / scope
Summary of object-oriented programming using Java
Studying Java ―― 3
[Java] array
Java protected
[Java] Annotation
[Java] Module
Java array
Studying Java ―― 9
Java scratch scratch
Java tips, tips
Java methods
Java method
java (constructor)
Java array
[Java] ArrayDeque
java (override)
java (method)
Java Day 2018
Java string
java (array)
Object-oriented programming
Java static
Java serialization
java beginner 4
JAVA paid
Studying Java ―― 4
java shellsort
[Java] compareTo
java reflexes
java (interface)
Java memorandum
☾ Java / Collection
Java array
Studying Java ―― 1
[Java] Array
[Java] Polymorphism
Java review
java framework
Java features
[Java] Inheritance
FastScanner Java
Java features
java beginner 3
Java memo
java (encapsulation)
Java inheritance
[Java] Overload