【发布时间】:2012-02-07 11:31:32
【问题描述】:
public class Base{
protected String str;
public static final Base ERROR = new Base("error");
...
}
public class Derived extends Base{
public static final Derived OTHER = new DERIVED("other");
public Derived(String str) {
super(str);
}
}
Derived page = Derived.OTHER; //OK
page = (Drived)Derived.ERROR; //ClassCastException
那么我可以将静态成员变量从 Base 转换为 Derived 类吗?
【问题讨论】:
-
在投射时这是一个很好的做法,使用 instanceOf 来验证投射是否合适。
标签: java inheritance static