Java使用2种不同的对象类型作为类中的字段
我有一个类,它可以将不同的对象作为一个字段,用于以下类似的目的Java使用2种不同的对象类型作为类中的字段,java,inheritance,Java,Inheritance,我有一个类,它可以将不同的对象作为一个字段,用于以下类似的目的 public class Project { private int id; private String title; private String description; private Implementer implementer; ..... } 实现者可以是个人(具有firstName、lastName等字段)或公司(具有companyName、industry等字段)。我如
public class Project {
private int id;
private String title;
private String description;
private Implementer implementer;
.....
}
实现者可以是个人(具有firstName、lastName等字段)或公司(具有companyName、industry等字段)。我如何让实施者引用这两种类型(个人和公司)。注意,我不希望它们扩展公共基类,因为它们没有公共字段
谢谢创建一个标记:
更改类以实现此接口:
public class Company implements Implementor {
...
}
public class Individual implements Implementor {
...
}
现在,以下两项作业均有效:
Implementor implementor;
...
implementor = new Company();
implementor = new Individual();
如何使实现接口的两个类都成为多通关系中的targetEntities。我已经用@Entity对它们进行了注释,但是我得到了一个错误:“实现者引用了一个未知的实体”?
Implementor implementor;
...
implementor = new Company();
implementor = new Individual();