1 回答
TA贡献1853条经验 获得超6个赞
尝试:
import dreambot.libs.Library;
public class Helper<T extends Library> {
private T lib;
private Class<T> name;
public Helper(Class<T> name, Library lib) {
this.name = name;
this.lib = lib;
}
public Class<T> getName() { return name; }
public T getLib() {
return lib;
}
}
甚至更简单:
import dreambot.libs.Library;
public class Helper<T extends Library> {
private T lib;
public Helper(Library lib) {
this.lib = lib;
}
public Class<T> getName() { return lib.getClass(); }
public T getLib() {
return lib;
}
}
并且在调用构造函数时不要忘记钻石运算符:
public ArrayList<Helper> helper = new ArrayList<>(asList(
new Helper<>(SomeLib.class, new SomeLib()),
new Helper<>(SomeOtherLib.class, new SomeOtherLib())
));
可以简化为:
public ArrayList<Helper> helper = new ArrayList<>(asList(
new Helper<>(new SomeLib()),
new Helper<>(new SomeOtherLib())
));
添加回答
举报