【发布时间】:2012-09-12 10:51:57
【问题描述】:
我有以下课程
抽象测试
public abstract class AbstractTest {
protected int testVar = 10;
}
测试
public class Test extends AbstractTest {
int testVar = 5;
public static void main(String[] args) {
AbstractTest test = new Test();
System.out.println(test.testVar);//Prints 10
Test secondTest = new Test();
System.out.println(secondTest.testVar);//Prints 5
}
}
为什么上面的程序在第一种情况下打印10,在第二种情况下打印5,尽管它是同一类的对象,即Test()?
更新:
I am now confused about how memory is allocated to Object and its variables. As instance variable is getting changed based on Class which is behaviour of Static?
更新:1
Every object will have two variables so question of same memory allocation does not comes in to picture. Thanks.
【问题讨论】:
标签: java polymorphism