简体   繁体   中英

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. The problem is that this class declaration construction is invalid, because it corresponds following apply method:

@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.

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.

What you likely want to do is remove <T extends XYZClass> from your method declaration and overrides altogether.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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