key words:
this keyword
This uses one :
Explain directly in code:
Class Man{private int age;private String name; Man () {name= null;age = 0;} Man (String N)//Normal Direct print "blf,0", but change N to name, it will print null,0{name = n; } Changed to name although added to the reading, but the parameter name in the stack, the stack has a local variable name name, then the assignment of name, will only assign themselves to the value of themselves. like int x = 5; x = x;/*man (String name) {this.name = name;//this keyword means that the name of this object will go into heap memory, it will not conflict} */man (int a) {age = A; }man (int a,string n) {age = A;name = n;} void Run () {System.out.println (name+ "," +age);//actually omitted here this.name+ "," +this.age}}public class Main {public static void Main (string[] args) {Man Jo = new Man ("BLF"); Jo.run ();}}
so: When the name of the authority variable and member variable is the same, it can be distinguished by the keyword this, which represents the current object (this is the reference to the object to which the function belongs):
This represents (constructor) the object that the man belongs to Jo. Which object calls the function where this is located, then this represents that object.
PS: Only objects can call constructors
So, regular and read-strong code
Class Man{private int age;private String name; Man () {name= null;age = 0;} Man (String name) {this.name = name; } Man (int.) {this.age = age;} Man (int age,string name) {this.age = Age;this.name = name;} void Run () {System.out.println (this.name+ "," +this.age);}} public class Main {public static void main (string[] args) {mans Jo = new Man ("BLF"); Jo.run ();}}
Interpret this as a memory allocation diagram
This uses two:
This can call other constructors in the constructor
Error Demo:
Class Man{private int age;private String name; Man () {name= null;age = 0;} Man (String name) {this.name = name;} Man (int.) {this.age = age;} Man (int age,string name) {//The above has already been written, is it possible to make a call? The answer is the negation of this. Man (name);//this represents the object to which the object is to be initialized, but this is used first. Man (age);//This is not initialized yet, so it is the wrong}void run () {System.out.println (this.name+ "," +this.age)}} public class Main {public static void main (string[] args) {mans Jo = new Man ("BLF");//Here 2 parameters Jo.run ();}}
Modify
Class Man{private int age;private String name; Man () {name= null;age = 0;} Man (String name) {this.name = name;} Man (int.) {this.age = age;} Man (int age,string name) {this (name);//Call the above constructor, note: Must put first line, house this.age = age; Below is wrong, reason see this.age = age;} void Run () {System.out.println (this.name+ "," +this.age);}} public class Main {public static void main (string[] args) {mans Jo = new Man (BLF); Jo.run ();}}
Note: This rule: This can only be defined in the first row of the constructor, because initialization must first be performed
The following code, this if you drop the face
Man (int age,string name) {this.name = name;//initialization blfthis.age = age;//initialization 12this ("GGGG");//change BLF to GGGG, error}
Ps:this () do not make excessive calls.
The application of this
Import java.security.cert.trustanchor;class man{private int age;private String name; Man () {name= null;age = 0;} Man (String name) {this.name = name;} Man (int.) {this.age = age;} Man (int age,string name) {this (name);//Call the above constructor, note: see below this.age = age;} public void Run () {System.out.println (this.name+ "," +this.age);} /* Determine if two people are peers */public Boolean judge (man g) {return this.age = = g.age;//here for Jo2}}public class Main {public static void Main (string[] args) {Man Jo = new Man ("BLF"); Man Jo2 = new Mans ("BLF2"), Boolean flag = Jo2.judge (Jo), if (flag==true) System.out.println ("YES"); else System.out.println ("NO");}}
Java Learning lesson nineth (keyword one)