In the last post
Difference between class and instance
I wrote an article.
As an example, I made a car as a class and created a car instance with the name "Benz" as an instance, but I received a comment from the person who read the article that the explanation was rough, so I wrote the previous content I will write a covered article. (The previous article has been stripped down to make it easier for beginners to imagine classes and instances.)
Here is the part you pointed out.
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.
In this regard,
In the first place, Toyota has a blueprint of Toyota, and Benz has a blueprint of Benz, and it is rough to put them together in one Car class and make a car of Benz or Toyota from there. For example, in the case of Toyota Prius, the appearance is the same, but the instances are different in mileage, remaining amount of gasoline, etc., and even if they have the same appearance (same class, same type), the characteristics are different for each instance. This is the difference for each instance.
It was that. I thought that was true this time
--Class inheritance --Class variables --Instance variables
While touching on these three, it is more advanced than the previous article Aim to explain ** classes ** and ** instances **
Classes have the idea of class inheritance. If you rewrite the class part into a blueprint, it becomes "** inheritance of the blueprint **". Taking the previous article as an example, if you create a [Car2] blueprint (Car2 class) that inherits the [Car] blueprint (Car class), [Car2] The blueprint is
-[Car] What is defined in the blueprint -[Car2] What is defined in the blueprint
Both can be used.
For example, if the above [Car2] blueprint (Car2 class) is used as the [Nissan] blueprint (Nissan car class), ** Nissan car class is automatically inherited by the idea of inheritance without writing the contents defined in Car in the class (blueprint) **
This time, we will define it in advance.
--Car class --NissanCar_serena class
Is created so that the NissanCar_serena class inherits from the Car class. *** Car can run on the ground with a four-wheeled vehicle ** We will proceed on the premise that there is a common understanding.
serena
Is a Nissan car and my parents' car. I will put the image so that it is easy to imagine. Please note that this image is just an instance of the blueprint.Car.java
class Car {
static Boolean isHaving4tires = true; ///① Absolutely the car has four tires
static Boolean canDriveOnGround = true; ///② The car will definitely run on the ground
String name; ///③ Each instance has a different name
String color; ///④ Each instance has a different color
Boolean isHavingWindow; ///⑤ Each instance may or may not have a window.
public void setNameAndColor(String n, String c) {
this.name = n; ///⑥ I will set the name and color for the instance
this.color = c;
}
public void checkWindow(Boolean b) {
this.isHavingWindow = b ///⑦ I will set whether the instance has a window or not.
}
}
(1) I created a Boolean type variable that has 4 tires and decided that it is absolutely true. (2) I created a Boolean type variable that can run on the ground, and decided that it is absolutely true. (3) The name that can be changed for each instance is defined by a String type variable. (4) The color that can change for each instance is defined by a String type variable. (5) The information "whether there is a window or not" that can change for each instance is defined by a Boolean type variable. (6) A method for setting the name and color for each instance has been defined. (7) We have defined a method for setting the information "whether there is a window" for each instance.
Car.java
class NissanCar extends Car {
static String whichMadeByCompany = "Nissan"; ///①絶対にNissanで作成された車です
String whereBought; ///(2) It has information about "where it was purchased" that can be different for each instance.
public void setWhereBought(String w) {
this.whereBought = w; ///③ Set the instance where it was purchased.
}
}
(1) Defined where it was created with a String type variable, and decided that it was absolutely Nissan. (2) The information "where was purchased" that can change for each instance is defined by a String type variable. (3) A method for setting the name and color for each instance has been defined.
This completes the creation of the blueprint (class).
I will explain what is called a class variable while using the above blueprint (class). What is a class variable?
-** The contents of the variable do not change depending on the instance ** -** Common information within the class **
is
Therefore, the class variables referred to in the above class (blueprint) are
--static Boolean isHaving4tires = true; (having 4 tires means different instances, but not different) --static Boolean canDriveOnGround = true; (Running on the ground means different instances but no change)
--static String whichMadeByCompany = "Nissan"; (Which company created it does not change even though the instance is different)
Class name. Class variable name
You can get the value of the class variable by doing.
Let's get the value of a class variable using the above class (1) I want to get the value of the class variable ʻisHaving4tires` in the Car class.
Car.isHaving4tires //true
NissanCar_serena.isHaving4tires //true
→ NissanCar_serena is the inheritance destination of the Car class
(2) I want to get the value of the class variable whichMadeByCompany
in the NissanCar_serena class.
NissanCar_serena.whichMadeByCompany //" Nissan "
What is an instance variable?
-** Variables whose values may vary depending on the instance **
is. Therefore, the instance variable referred to in the above class (blueprint) is
--String name; (have different names for each instance) --String color; (can have different colors for each instance) --Boolean isHavingWindow (varies whether each instance has a window or not)
--String whereBought; (Information about where the purchase was made may differ for each instance)
Instance.variable name
Let's get the value of the instance variable using the above class
(1) Create an instance of Car class and NissanCar_serena class, set the name and color (Mike's car, red), and get the instance variables name
, color
.
test.java
void test() {
Car myCar = new Car();
myCar.setNameAndColor("Mike's car","Red");
System.out.print(myCar.name); //Access the instance myCar with the instance variable name"Mike's car"Get
System.out.print(myCar.color); //Access the instance myCar with the instance variable color"Red"Get
NissanCar_serena myCar2 = new NissanCar_serena();
myCar2.setNameAndColor("Mike's car","Red");
System.out.print(myCar2.name);//Access the instance myCar2 with the instance variable name"Mike's car"Get
System.out.print(myCar2.color);//Access the instance myCar2 with the instance variable color"Red"Get
}
}
(2) Create an instance of the NissanCar_serena class, set the information of where you bought it, and get the instance variable whereBought
.
test.java
void test2() {
NissanCar_serena myCar2 = new NissanCar_serena();
myCar2.setWhereBought("Tokyo");
System.out.print(myCar2.whereBought);//Access the instance myCar2 with the instance variable wherebought"Tokyo"Get
}
}
As I wrote this article, I was struck by the ** instance variables of the NissanCar_serena class that I inherited from **.
As explained above
** Instance variables are different for each instance ** ** Class variables are different for each class **
is.
When I tried to define an instance variable, when I imagined the difference between instances in the NissanCar_serena class, which is not in Car, NissanCar is a commercial car called Nissan, while Car class is still commercial or not. Since it is unknown, we will use the difference to define where it was bought,
public String whereBought = "";
Was defined. The possibility is very small, but there is a possibility that people who build a car from scratch and use it for themselves (laugh) However, if you have a class as a blueprint and you are asked if you need to define "where it was purchased" information as its variable, you can't say anything. .. ..
Also defined in the Car class
public Boolean isHavingWindow;
Regarding the instance variable, I would like you to imagine the following image.
Thank you for browsing. I would appreciate it if you could comment if you have any.
Recommended Posts