Always thought the This keyword refers to the caller object, but this time we really understand that this represents the current object, but pointing to the caller object is actually a polymorphic usage, as follows: B inherits A, invokes the Class A method in B, and uses this to access the member variables and methods in a. If you use this to access a member variable, such as THIS.S, whether or not the B subclass has an S attribute, or if the modifier of S is private or public, THIS.S will always print the value of the S attribute of the current class, because the member variable cannot be overridden and cannot be overwritten; Besides, use this to visit Ask the method as follows: In a this.test (), note that at this point, this represents Class A, but points to B, such as: a A = new B (); Because class B overrides the test method of the parent class, the test method of B is invoked, and if the test method of Class A is changed to a private type, then This.test () invokes the test method for Class A, because while Class B also has the test method, this A method is not a method of overriding Class A, it is a unique method, because the parent class is private, the subclass cannot own, salute.
public class Helloa {public
static void Main (string[] args) {
new B (). print ();
}
Class B extends a{
private String s = "B";
public void print () {
super.print ();
}
Public String Test () {return
"Test_b";
}
}
Class A {
private String s = "a";
public void print () {
System.out.println (THIS.S);
System.out.println ( this.test ());
}
Public String Test () {return
"test_a";
}
}
Printing results are:
A
Test_b
1, the Print method of Class A is called in B, and the S property and test method are invoked in a, at which point, this is a B object, but the property in a is printed out, and the method of Class B is called, stating that the method can be overridden and the property cannot override
2, if the test method of Class A is changed to private, the test method of Class A is invoked because the test method in Class B is not the test method of the overridden Class A, which can be said to be a new method because the test method of Class A is private