This
There must always be something to represent the current object of the class. Just like this pointer in C++, the this keyword in Java represents the reference to the current object.
It has three main functions:
1. Call other constructors in the constructor.
For example, there is a Student class with three constructors. If you call another constructor in a certain constructor, this() is required, and it is not possible to use Student() directly.
2. Return the reference to the current object.
3. Distinguish between member variable names and parameter names.
See the following example:
public class Student { private String name; private int age; private String college; public Student() { age = 20; } public Student(String name) { this();//can not be call Student,only use this() method. this.name = name; System.out.println("this student name is "+name); } public Student(String name,String college) { this(name);//C++ you can directly call other constructors this.college = college; System.out.println("this student name is "+name+" college is "+college); } public Student upgrade() { age++; return this; } public void print() { System.out.println("name is: "+name +" age is: "+age +" college is: "+college); } public static void main(String[] args) { Student student1 = new Student("linc"); Student student2 = new Student("linc","shenyang college"); student2.upgrade().print(); } }When you are lost in the vast ocean of objects, don’t forget to use this to find yourself.
super
super is this father. From the perspective of facial objects, these two concepts are easy to understand.
Subclasses are inherited from the parent class. The protected and above properties and methods of the parent class are born with innately in the subclass. So, why do you still need the keyword super?
First, look at the construction of the parent class. When constructing the subclass, you must first call the default constructor of the parent class, which is consistent with the construction properties of C++. When the parent class has multiple constructors, you need to specify which one to call. This requires super(arg1,arg2...).
It should be noted that when calling the base class constructor in the constructor of the subclass, super must be written as the first, otherwise an error will be reported.
Second, call this method of the parent class in some methods that subclass overwrites the parent class. As we all know, some methods of overwriting parent classes in subclasses are a way to oriented polymorphism in object-oriented, and for various other reasons, this method of the parent class needs to be called in this method to distinguish. At this time, super needs to be used to complete it.
public class ClassLeader extends Student { private String duty; public ClassLeader() { duty = "class monitor"; } public ClassLeader(String duty,String name,String college) { super(name,college); this.duty = duty; } public void print() { super.print(); System.out.println("duty is " + duty); } public static void main(String[] args) { ClassLeader leader = new ClassLeader("life","linc","shenyang"); leader.print(); } }Place the two class files in the same directory, compile and run:
D:/workspace/Java/project261/super>java -d . *java D:/workspace/Java/project261/super>java ClassLeader
Running results:
This student name is linc this student name is linc college is shenyang name is: linc age is: 20 college is: shenyang duty is life
See how it is handled in other languages:
C# provides the base keyword to complete the functions similar to super, and C++ directly calls it with the name of the base class.