<分区>
假设我有以下类(class):
class Parent
{
private int ID;
private static int curID = 0;
Parent()
{
ID = curID;
curID++;
}
}
还有这两个子类:
class Sub1 extends Parent
{
//...
}
和
class Sub2 extends Parent
{
//...
}
我的问题是这两个子类共享同一个静态 curID 成员 父类,而不是有不同的父类。
如果我这样做:
{
Sub1 r1 = new Sub1(), r2 = new Sub1(), r3 = new Sub1();
Sub2 t1 = new Sub2(), t2 = new Sub2(), t3 = new Sub2();
}
r1、r2、r3 的 ID 为 0、1、2,t1、t2、t3 的 ID 为 3、4、5。 而不是这些,我希望 t1、t2、t3 具有值 0、1、2,即使用 curID 静态变量的另一个副本。
这可能吗?如何?