0

如果我有一个适合泛型的类,有没有办法包装这种丑陋?

所以这

public HasOne aMethod(Class<? extends Model> clazz){

    HasOne<Customer> hasOne = injector.getInstance(Key.get(new TypeLiteral<HasOne<Customer>>() {
    }));

    return hasOne;
}

会变成这样的东西。(这显然不能编译)

public HasOne aMethod(Class<? extends Model> clazz){

    HasOne<clazz> hasOne = injector.getInstance(Key.get(new TypeLiteral<HasOne<clazz>>() {
    }));

    return hasOne;
}
4

2 回答 2

4

为什么不

public <T extends Model> HasOne<T> aMethod(Class<T> clazz){

    HasOne<T> hasOne = injector.getInstance(Key.get(new TypeLiteral<HasOne<T>>() {
    }));

    return hasOne;
}
于 2013-06-20T04:17:15.663 回答
0
public <T extends Model> HasOne<T> aMethod(Class<T> clazz){
    HasOne<T> hasOne = injector.getInstance(Key.get(new TypeLiteral<HasOne<T>>() {
    }));
    return hasOne;
}

HasOne<Customer> customer = aMethod(Customer.class);
于 2013-06-20T04:21:09.227 回答