我注意到以下关于Java的内容:
如果我创建一个类:
class IntStorage
{
public int i;
public void setInt(int i) { this.i = i; }
public int getInt() { return i; }
}
继承它,并存储其他数据:
class IntAndStringStorage : IntStorage
{
public String s;
public void setString(String s) { this.s = s; }
public String getString() { return s; }
}
我会这样做:
IntAndStringStorage IandSstorage = new IntAndStringStorage();
IandSstorage.setString("Test");
IntStorage Istorage = (IntStorage)IandSstorage;
IntAndStorageStorage test = (IntAndStorageStorage)Istorage;
System.out.println(test.getString());
即使我将其转换为继承类,它仍然是完全有效的。现在我假设信息仍然存在于对象中,那么我能否完全将其转换为继承类而不保留旧信息?我不想在我的int
存储类中留下String
存储。
class IntAndStringStorage : IntStorage
这是 Java 8 的表示法吗?在这里它无法编译。 - Andrew ThompsonIntAndStringStorage
不是IntStorage
。在继承之前三思。 - yzn-pku