我有一個泛型類,它聲明某些字段和一個構造函數,可與他們:Java繼承:隱藏字段
public abstract class GenericClass extends JFrame
{
protected static String FIELD_1;
protected static String FIELD_2;
protected static String FIELD_3;
public GenericClass()
{
super(FIELD_1+" "+FIELD_2+FIELD_3);
}
}
類和子類應該隱藏字段和使用超類的構造函數:
public class ChildClass1
{
protected static String FIELD_1 = "hello";
protected static String FIELD_2 = "world";
protected static String FIELD_3 = "!";
public ChildClass
{
super();
}
}
public class ChildClass2
{
protected static String FIELD_1 = "another";
protected static String FIELD_2 = "class";
protected static String FIELD_3 = "!";
public ChildClass
{
super();
}
}
我不明白爲什麼創建的JFrames有標題null nullnull
。我究竟做錯了什麼?
更新
使用這些類的很簡單:
public class Main
{
public static void main(final String[] args)
{
new ChildClass1();
new ChildClass2();
}
}
您已經列出了三個類,但沒有使用它們的代碼。你如何使用這些類? – Rajit
另請參見:[覆蓋超類'實例變量](http://stackoverflow.com/questions/427756/overriding-a-super-class-instance-variables) – Rajit
@Rajit雖然這與我的問題無關,請參閱update –