In this article, we aim to understand the nuances of classes, not the implementation or instantiation of them.
Blueprint. Or something like a concept defined to create an entity.
An entity created based on a blueprint called a class. Take a car as an example We manufacture a ** instance ** (entity) called Benz from the ** class ** (design document) of the car. Each instance has different characteristics. If you make a black car, some of them will be Toyota cars, some will be Mercedes-Benz cars, they will be different in size and shape, and no instance will be the same.
Here we use ** java **. We will create a car design document called a car class and create an instance.
First, let's create a car class design document. Please refer to the following page for the specific method of creating the design document. (Please wait as it is still under construction)
Car.java
class Car {
//Field declaration
public String name = ""; //Initial value of the name ・ ・ ・ 1
public String color = ""; //Initial color value ・ ・ ・ 2
public int speed = 0; //Initial value of speed ・ ・ ・ 3
//Method declaration
public void setCar(String n, Sring c) { //・ ・ ・ 4
this.name = n; //Set the instance name ... 4-1
this.color = c; //Set the instance color ... 4-2
}
public void setSpeed(int a) {// 5
this.speed = a;//Initial value of speed ・ ・ ・ 5-1
}
}
Even if you don't know java, you can understand it intuitively. I made a car design document (class). The defined contents are as follows
This is just a design document, so unless you create an instance I just created a design document.
This time, create a car instance based on the created design document.
--The name is Benz --The color is white --Speed is 100
will do.
In the Test.Java class, as shown below Add a method called makeCar and write it in it.
Test.java
public void makeCar() {
Car myCar = new Car(); //・ ・ ・ 1
myCar.setCar("Benz","white"); //・ ・ ・ 2
myCar.setSpeed(100); //・ ・ ・ 3
}
}
The contents defined in this makeCar are
When processing object-oriented in languages such as Java and objective-c, the words ** class ** and ** instance ** will appear repeatedly.
If you don't know, it's something you usually have around you, and you may find it easier to create a class or instance and replace it.
I wrote an article below that explains the difference between instance methods and class methods. I hope you can refer to that as well. (making)
If you have any suggestions, please let me know! !!