Previously thought the This keyword refers to the caller object, but this time really understand that this represents the current object, but point to the caller object, is actually a polymorphic usage, as follows: B inherits A, in B calls a method of Class A, in a to access member variables and methods, at this time, If you use this to access member variables, such as the following, THIS.S, whether the B subclass has an S property, or the modifier of S is private or public, THIS.S will always print the current Class S property value, Reason: member variable cannot be overridden, cannot overwrite; 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 called, and if the test method of Class A is changed to a private type, then This.test () calls the test method of Class A, because at this point the Class B also has the test method, but this A method is not a method of overriding Class A, it is a unique method, because the parent class is private, and the subclass cannot have it, salute!
[Java]View PlainCopy
- Public class Helloa {
- public static void Main (string[] args) {
- new B (). print ();
- }
- }
[Java]View PlainCopy
- class b extends a{
- private String s = "B"  ;  
- public void print () {
- super.print ();
-     }  
- public string test () {
- return "Test_b";
- }
- }  
[Java]View PlainCopy
- 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, in B called the Class A print method, in a in the S property and the test method, this is a B object, but printed out a property in a, called the Class B method, the method can be overridden, the property cannot be overridden
2, if the Class A test method is changed to private, then the test method of Class A is called, because the test method in Class B is not the overridden test method of Class A, it can be said to be a new method because the test method of Class A is private
Java this subclass invokes the parent class, and the parent class then calls the methods and properties that the subclass has overridden (again understanding)