Public class test2 {public string user = "test2";
Public Test2 () {}
Public string getuser () {system.out.println (this.user); // Why is it "Test2" instead of "test4" user = getpassword (); // Why is it performed by TEST3, Instead of itself, Return User;} public string getpassword () {system.out.println (this.user); return "hello test2";}}
Public class test3 extends test2 {public string user = "test3"; public test3 () {}
Public string getpassword () {system.out.println (this.user); return "Hello Test3";}}
Public class test4 extends test3 {public string user = "test4"; public test4 () {} public static void main (string [] args) {test4 t4 = new test4 (); t4.getuser ();}
The output result is: Test2, Test3 is given the split answer:
1. This is a problem of scope! Due to the inheritance, it is only for the method's coverage, and there is no coverage for attributes.
In that, the user has 3 properties, there are 3 copies in a TEST4 object!
Within Test4's role, he is TEST4, TEST3 is the range of Test3.
This problem may be more difficult to understand. But you think that you will understand the code of the code!
If you have learned C, you must know that the side effects brought by the global variable, haha, similar to this!
But this problem, it seems that it is not a general problem. When I use JB to track, the user has always been Test4, but the actual output is not!
I don't know how JB's debugging tool, how to think about this problem! 2. This is the handle of the object of the current call method, which is Test4 in this example, and it has three USER attributes, which are of their own attribute domains, that is, "Test2" in Test2, that is, only in Test3 Display "Test3" (if you also rewrite a getUser () in Test3, "Test3" will be displayed, it feels that the three properties only show the corresponding one in their own maidens; for the method, just call the nearest call A method of parent class, so calling the getPassword () method of Test3.