Good evening. Again. Thank you for reading and commenting on many articles the other day. I was honestly surprised because I didn't expect to see it so far. And I'm sorry I posted an article with childish content. I will get used to it from now on, so I hope you can put up with it a little more and get along with me.
This time I will write about instances. As I received your comment, the program for making cats is different from the original program because object-oriented programming is for writing instructions, but this time I will use cats for the sake of clarity. Also, like the other day, I will write the text I used to study java in the references, so please read it. Now, let's write a program to make many cats as an example.
Cat.java
public class Cat{
String name;
int onaka;
static int esa = 10;
Cat(){
}
Cat(String name){
this.name = name;
}
void run(){
System.out.printlf(name + "Ran");
name.onaka -= 10;
}
void gohan(int eat){
this.esa -= eat;
this.onaka += eat;
}
}
The cat has a name and is hungry. I'm hungry when I run. Also, since cats eat rice, we defined the remaining amount of rice in the house. Eating rice will satisfy your hunger. Also, Cat () and Cat (String name) are called constructors, which are called when an instance of Cat is created. Instances are described below. Therefore, when instantiating Cat, Cat () that does nothing if there is no argument is called, and Cat (String name) that sets the name if there is a String type argument is called. Creating methods with different arguments and type order, even if they are the same method, is called overloading.
Main.java
public class Main{
public static void main(String[] args){
Cat a = new Cat();
a.name = "kuro";
a.onaka = 50;
Cat b = new Cat("shiro");
b.onaka = 30;
a.run();
a.gohan(5);
}
}
I will explain this Main.java. The right side of the third line creates an instance of the Cat class. By using an instance, you can create two types of cats with the following description, and each can have a value. The instance created here is assigned to the Cat type variable a on the left side. In the two lines below that, the cat in a is named kuro and the hunger is set. Next, on the left side of the 6th line, shiro is included in the value to be passed. This calls Cat (Sting name) on line 9 of the Cat class, where we set the name of cat b. After that, the hunger level of cat b is set. The next line calls the run () method to run cat a, which reduces the hunger of cat a by 10 as described in the run () method of the Cat class. After that, cat a is eating 5 rice. This is the point of this time, but the esa field of the Cat class has something called static. If static is attached to the field, it means that the value is shared by all instances, so 95 is displayed regardless of whether cat a or cat b outputs the remaining amount of esa. static is convenient! !! !! !! !! That's all for today
I was able to put together an article in a relatively short time today. No way I could write so quickly in yesterday's day ... I don't have any plans tomorrow, so I'll study Vue.js in the morning and write an article in the afternoon. Tomorrow I will probably write about encapsulation. Thank you for reading this far. I would appreciate it if you could comment if there are any mistakes or strange expressions.
[Introduction to Java 2nd Edition (Refreshing Series)](https://www.amazon.co.jp/%E3%82%B9%E3%83%83%E3%82%AD%E3%83%AA % E3% 82% 8F% E3% 81% 8B% E3% 82% 8BJava% E5% 85% A5% E9% 96% 80-% E7% AC% AC2% E7% 89% 88-% E3% 82% B9 % E3% 83% 83% E3% 82% AD% E3% 83% AA% E3% 82% B7% E3% 83% AA% E3% 83% BC% E3% 82% BA-% E4% B8% AD% E5% B1% B1-% E6% B8% 85% E5% 96% AC / dp / 484433638X / ref = zg_bs_515820_1? _Encoding = UTF8 & psc = 1 & refRID = JGCC33P1VGV3V5VKQGQR)
Recommended Posts