0

我以为我了解如何使用.classClass<>但我想不是。下面super(ApprovalWorkstation.class没有被映射到构造函数。我如何确定我正在传递一个类引用,以便基础WorkstationRequest可以实例化它?

public class ApprovalWorkstation extends Workstation {

    public ApprovalWorkstation(WorkstationEntity entity) {
        super(entity);
    }
}

public class WorkstationRequest extends com.production.socket.request.WorkstationRequest {
    public WorkstationRequest() {
        super(ApprovalWorkstation.class); //unable to map to constructor
    }
}

这是WorkstationRequest上面扩展的基础

public class WorkstationRequest {
    private Class<Workstation> workstationClass;

    public void WorkstationRequest(Class<Workstation> workstationClass) {
        this.workstationClass = workstationClass;
    }

更新

我很抱歉造成混乱,我的构造函数实际上有Class<Workstation>,而不是Workstation像我最初那样。

4

4 回答 4

4

为了能够同时传递Workstation类及其子类,您应该将定义更改WorkstationRequest为以下内容:

public class WorkstationRequest {
    private Class<? extends Workstation> workstationClass;

    public WorkstationRequest(Class<? extends Workstation> workstationClass) {
        this.workstationClass = workstationClass;
    }
}
于 2013-05-13T17:26:16.070 回答
0

如果我错了,请纠正我,但是您正在传递一个Workstation实例,而需要的是Class<Workstation>. 也许workstationClass.getClass()改用?

于 2013-05-13T17:20:50.230 回答
0

您正在尝试将一个实例分配给Worstation类型Class<Workstation>显然不一样的字段。

你可能想要做的是:

public WorkstationRequest(Class<Workstation> workstationClass) {

当然void,构造函数中没有返回类型 ()。您的“构造函数”只是一种方法:

public void WorkstationRequest(Class<Workstation> workstationClass) { // method
public      WorkstationRequest(Class<Workstation> workstationClass) { // constructor
于 2013-05-13T17:21:01.063 回答
0
  1. 在基础 WorkstationRequest 类中没有将 aClass作为参数的构造函数。
  2. 构造函数没有返回类型,因此public void WorkstationRequest需要更改为public WorkstationRequest.
于 2013-05-13T17:22:07.223 回答