考慮下面的類後失去其獨特的性能:是否子對象鑄造來回父類之間
public class Phone {
private boolean has3g;
public boolean has3g() {
return has3g;
}
public void setHas3g(boolean newVal) {
has3g = newVal;
}
}
public class Blackberry extends Phone {
private boolean hasKeyboard;
public boolean hasKeyboard() {
return hasKeyboard;
}
public void setHasKeyboard(boolean newVal) {
hasKeyboard = newVal;
}
}
如果我是創造的Blackberry
的情況下,其轉換爲Phone
對象,然後把它重新回到Blackberry
,原來的Blackberry
對象會失去它的成員變量嗎? E.g:
Blackbery blackbery = new Blackberry();
blackbery.setHasKeyboard(true);
Phone phone = (Phone)blackbery;
Blackberry blackberry2 = (Blackberry)phone;
// would blackberry2 still contain its original hasKeyboard value?
boolean hasKeyBoard = blackberry2.hasKeyboard();
你試過了嗎?你做了什麼事? – Kal 2013-04-25 20:39:27
我確實嘗試過,它工作正常 – 2013-04-25 20:46:18