简体   繁体   English

如何使用泛型类型声明类-Java 1.7

[英]How to declare class using generics types - Java 1.7

I have following class: 我有以下课程:

public class AClass<T extends XYZClass> extends BFunction<DTOClass, T>{
    @Override
    public <T extends XYZClass> T apply(DTOClass input) {
        return null;
    } 
}

where T is generic Type, BFunction is function that implements com.google.common.base.Function with apply method. 其中T是泛型类型,BFunction是使用apply方法实现com.google.common.base.Function的函数。 The problem is that this class declaration construction is invalid, because it corresponds following apply method: 问题在于该类声明构造无效,因为它对应于以下apply方法:

@Override
public T apply(DTOClass input) {
    return null;
}

I also tried: 我也尝试过:

public class AClass extends BFunction<DTOClass, <T extends XYZClass> T>{
    @Override
    public <T extends XYZClass> T apply(DTOClass input) {
            return null;
    }
}

but this syntax is invalid. 但是此语法无效。 Could you advise me how to correct my class declaration? 您能建议我如何更正我的班级声明吗?

Remove the type variable declared on the method: 删除在方法上声明的类型变量:

public class AClass<T extends XYZClass> extends BFunction<DTOClass, T>{
    @Override
    public /* remove this */ T apply(DTOClass input) {
        return null;
    } 
}

You're trying to declare another type variable called T which hides the type variable declared on the class. 您正在尝试声明另一个称为T的类型变量,该变量将隐藏在类中声明的类型变量。

You are declaring a generic method in your parent class, where the <T extends XYZClass> clause has nothing to do with the <T extends XYZClass> in your class declaration. 您声明一个通用的方法,在你的父类,其中<T extends XYZClass>条款无关与<T extends XYZClass>在你的类声明。

What you likely want to do is remove <T extends XYZClass> from your method declaration and overrides altogether. 您可能想要做的是从方法声明中删除<T extends XYZClass>并完全覆盖。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM