Object-oriented
A programming model of artificial abstraction. When understanding object-oriented code, we should understand it according to the abstract model. We should not only understand complex problems literally from the code, but also learn to split them into an independent small problem. By solving each small problem, we can finally solve a big problem.
class
Classes are abstractions of things, algorithms, logic, concepts, etc. They are used to encapsulate data and method codes related to these types, and to encapsulate them into independent components. They can be interpreted as templates or drawings. When creating class objects, they are equivalent to creating them according to this template.
object
Specific instances created from classes occupy separate memory space and store their own attribute data. Instances can be controlled separately to execute the code in the specified method.
Quote
Save the memory address of an instance object with reference variable, assign the address value to the variable, and call an instance with variable, which can be understood as remote control, only need to operate with reference.
Here is an example to illustrate:
Project: Day 0101 Soldier
Classes: day0101.Test1 and Solder
Solder class code:
1 package day0101; 2 3 4 5 import java.util.Random; 6 7 8 9 /* 10 11 * encapsulation 12 13 * Relevant data, logical operation method, 14 15 * Encapsulated as a "class" component 16 17 */ 18 19 public class Solder { 20 21 //Membership variables 22 23 int id; //Default value 0 24 25 int blood = 100; 26 27 28 //Membership method 29 30 public void go() { 31 32 System.out.println(this.id+"Soldier No. 1 marches forward"); 33 34 } 35 36 public void attack() { 37 38 if (blood == 0) { 39 40 System.out.println( 41 42 "This is"+id+"The Corpse of Soldier Number One"); 43 44 return;//That's the end of the method. 45 46 } 47 48 System.out.println(id+"Soldier No. 1 Attacks"); 49 50 //Simulated injury,Decreased blood volume 51 52 //Generate a random blood loss 53 54 int d = new Random().nextInt(10); 55 56 blood -= d;//Decreased blood volume 57 58 if (blood < 0) {//If the blood volume turns negative,Reset to 0 59 60 blood = 0; 61 62 } 63 64 System.out.println("Blood volume: "+blood); 65 66 if (blood == 0) { 67 68 System.out.println(id+"Soldier No. 1 was killed in battle"); 69 70 } 71 72 } 73 74 }
Test1 class code:
package day0101; public class Test1 { public static void main(String[] args) { //Newly build Soldier Example //Then put its memory address value,Save to s1 variable /* s1 = { * id: 0, * blood: 100 * } */ Solder s1 = new Solder(); /* s2 = { * id: 0, * blood: 100 * } */ Solder s2 = new Solder(); //use s1 Variable finds soldier's memory space,Visiting it id variable s1.id = 9527; s2.id = 9528; //use s1 Variable finds soldier's memory space,Let it execute go()method code s1.go(); s2.go(); s2.attack(); s1.attack(); } }
Construction method
A special method to execute when creating a new instance: new Solder()
Java classes must have constructors, and if they do not define constructors themselves, the compiler adds default empty constructors
class Solder{ public Solder(){ } }
The above code is an empty constructor that you add. When you don't add it, the compiler automatically generates the empty constructor and does nothing.
The Function of Structural Method
After an instance is created, a special method is executed immediately. The code in the construction method can be written arbitrarily to complete any function or perform any operation. The general function of the construction method is to assign values to the member variables. The member variables are variables defined in the class.
this usage
this.id
id is a member variable, when a constructor is used to assign a member variable:
public class A{ int id; public A(int id){ this.id=id; } }
this.id represents the member variable ID.
this(a,b)
a,b are parameters in parentheses, and this(a,b) means that the calling parameter is constructed by two methods
public class A{ int id; int a; public A(int id){ this(id,5) } public A(int id,int a){ this.id=id; this.a=a ; } }
this() is at the beginning of the code.