用Java实现继承类型接口
如何在界面中创建addWorker?OfficeWorker继承自Worker,现在我想为几个Worker创建接口。我不知道怎么做才对。void addWorker(Worker-Worker)和void addWorker(Worker-Worker)错误 接口用Java实现继承类型接口,java,inheritance,interface,Java,Inheritance,Interface,如何在界面中创建addWorker?OfficeWorker继承自Worker,现在我想为几个Worker创建接口。我不知道怎么做才对。void addWorker(Worker-Worker)和void addWorker(Worker-Worker)错误 接口 public interface TypeOfWorker { void addWorker(Worker worker); } oficeworker public class OfiiceWorker e
public interface TypeOfWorker {
void addWorker(Worker worker);
}
oficeworker
public class OfiiceWorker extends Worker implements TypeOfWorker {
private int officeID;
private int intelectl;
private List<OfiiceWorker> ofiiceWorkers = new ArrayList<>();
public OfiiceWorker(String name, String surname, int age, int experience, String street, int building,
int local, String city, int officeID, int intelectl) {
super(name, surname, age, experience, street, building, local, city);
this.officeID = officeID;
this.intelectl = intelectl;
}
@Override
public void addWorker(OfiiceWorker ofiiceWorker) {
ofiiceWorkers.add(ofiiceWorker);
}
}
public abstract class Worker {
private int identifier;
private String name;
private String surname;
private int age;
private int experience;
private String street;
private int building;
private int local;
private String city;
public Worker() {
}
public Worker(String name, String surname, int age, int experience, String street, int building, int local,
String city) {
setIdentifier();
this.name = name;
this.surname = surname;
this.age = age;
this.experience = experience;
this.street = street;
this.building = building;
this.local = local;
this.city = city;
}
public void setIdentifier() {
this.identifier = (int) (System.currentTimeMillis() / 1000);
}
}
您应该使用泛型让编译器轻松处理您的类型。首先,使您的界面通用:
public interface TypeOfWorker<T extends Worker> {
void addWorker(T worker);
}
您应该使用泛型让编译器轻松处理您的类型。首先,使您的界面通用:
public interface TypeOfWorker<T extends Worker> {
void addWorker(T worker);
}
也许你应该解释一下你想要用你的界面实现什么,否则它将很难帮助你。此外,您当前的解决方案看起来像一个糟糕的设计,OficeWorker的每个实例都将包含一个其他实例的列表。为什么你想要这个,这个列表不应该在另一个类中,而应该像某种工作者处理程序类一样。也许你应该解释一下你想用你的接口实现什么,否则它将很难帮助你。此外,您当前的解决方案看起来像一个糟糕的设计,OficeWorker的每个实例都将包含一个其他实例的列表。为什么您想要这个,这个列表不应该在另一个类中,而应该像某种工作者处理程序类一样。
@Override
public void addWorker(OfiiceWorke worker) {