多级泛型

时间:2013-06-16 07:10:43

标签: java class generics

我正在尝试写一个类:

public class Article<T<? extends ManageGenericArticleInDTO>, U> implements Serializable {
    private T<? extends ManageGenericArticleInDTO> userInput;
    private U severData;

    public Article<T<? extends ManageGenericArticleInDTO>, U>() {
        super();
    }


    public void setUserInput(T<? extends ManageGenericArticleInDTO> userInput) {
        this.userInput = userInput;
    }

    public T<? extends ManageGenericArticleInDTO> getUserInput() {
        return userInput;
    }

    public void setSeverData(U severData) {
        this.severData = severData;
    }

    public U getSeverData() {
        return severData;
    }
}

这样我就可以写下这些:

Article<UpdateManageArticleInDTO<ManageGenericArticleInDTO>, UpdateCentralPOSMArticleDTO> article = new Article<UpdateManageArticleInDTO<ManageGenericArticleInDTO>, UpdateCentralPOSMArticleDTO>();
Article<AddManageArticleInDTO<ManageCentralPOSMArticleInDTO>, AddArticleStepDTO> article = new Article<AddManageArticleInDTO<ManageCentralPOSMArticleInDTO>, AddArticleStepDTO>(); // ManageCentralPOSMArticleInDTO extends ManageGenericArticleInDTO

我收到Error(7,32): > expectedError(7,33): illegal start of type等编译错误。在我的理解中,Type引用将被实际的类替换。,我不能在这里定义类吗?


如果我这样做:

public class AddArticleInBean<T, U> implements Serializable {
    private T userInput;
    private U severData;

    public AddArticleInBean() {
        super();
    }


    public void setUserInput(T userInput) {
        this.userInput = userInput;
    }

    public T getUserInput() {
        return userInput;
    }

    public void setSeverData(U severData) {
        this.severData = severData;
    }

    public U getSeverData() {
        return severData;
    }
}

此错误不再出现,但我收到警告,T和U是不可序列化的。

1 个答案:

答案 0 :(得分:1)

我认为你缺少开始和结束括号

应该是,

Article<UpdateManageArticleInDTO<ManageGenericArticleInDTO>, UpdateCentralPOSMArticleDTO> article = new Article<UpdateManageArticleInDTO<ManageGenericArticleInDTO>, UpdateCentralPOSMArticleDTO>();
Article<AddManageArticleInDTO<ManageCentralPOSMArticleInDTO>, AddArticleStepDTO> article = new Article<AddManageArticleInDTO<ManageCentralPOSMArticleInDTO>, AddArticleStepDTO>();