[Java] Object-Oriented Summary \ _Part 1 -Qiita
Last time, it was about class creation and instantiation.
This time, we will perform instance operation and finish once.
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
[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
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.
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.
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.
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.
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