1 classPersonDemo32 {3 Public Static voidMain (string[] args)4 {5Person p=NewPerson ("Zhang San", 22);6 }7 }8 9 /*Ten This statement One 1. Used when calling each other between constructors, cannot be called in a generic function A The 2.this statement must be the first sentence in the constructor and can be used only once in a constructor - */ - //The This keyword denotes the object where the function is located, which marks the members of the object, avoids ambiguity and errors, and facilitates reading the class Person - { - Private intAge ; - PrivateString name; +Person ()//no parameter constructor - { +System.out.println ("A:name:" + This. name+ "," + "Age:" + This. age); A at } -Person (String name)//a parameter constructor - { - This. name=name; - //System.out.println ("B:name:" +this.name+ "," + "Age:" +this.age); - in } -Person (String name,intAge//two parameter constructors to { + //This.name=name; - This(name);//call the constructor with the this statement the This. age=Age ; *System.out.println ("C:name:" + This. name+ "," + "Age:" + This. age); $ Panax Notoginseng } -}
Output Result:
C:name: Zhang San, age:22
This keyword is simple to apply