我正在设置一个简单的例子来展示继承和多态性.我不知道如何解释我在Android Studio调试器中看到的行为. 我在一个Empty View Activity项目中声明了这两个简单的类:
public class FirstClass {
public String whoAmI = "FirstClass";
int complexCalculation = 0;
public FirstClass(){
System.out.println("FirstClass Constructor");
complexCalculation = 3^6^8^9^3;
}
}
和
public class SecondClass extends FirstClass{
public String whoAmI = "SecondClass";
public SecondClass(){
System.out.println("SecondClass Constructor");
}
public void printWhoAmI(){
System.out.println(whoAmI);
}
}
在MainActivity中:
SecondClass test = new SecondClass();
FirstClass tester= new FirstClass(); //What will be printed to log?
tester = test;
tester.whoAmI = "DoingStuff";
When placing a breakpoint on the last line 和 looking at the variables state, I see: Why doesn't the attribute itself change? Where can I further read about the behavior displayed here?