如果父亲是 parent , parent 是一个人,而一个人有一个父亲,我将创建以下内容:
class Person{
Father father;
}
class Parent extends Person{}
class Father extends Parent{}
实例:
Person p1 = new Person();
Person p2 = new Person();
p1.father = p2; //father is of the type Father
这不起作用...现在尝试强制转换::
Person p1 = new Person();
Person p2 = new Person();
p1.father = (Father)p2;
这也不起作用。
什么对这个案例有用?
最佳答案
其实,父亲并不是人的子类。这只是一种关系。
class Person {
Person father;
}
关于java - OOP扩展继承问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2983798/