问题描述
假设我有:
public class Components< T>扩展了TupleList< Class< T>,String> {
private static final long serialVersionUID = 1L;
$ b $ public void add(Class classe,String name){
this.add(new Tuple< Class< T> String>(classe,name));
}
}
我希望能够做到以下几点:
组件< IFilter>引擎=新组件< IFilter>(){{
add(FilterEngineIdentity.class,Identity);
}};
其中 FilterEngineIdentity实现了IFilter
。我如何实现这个功能而不将我的类 Components
绑定到更具体的类定义上?
编辑:它的工作原理! 以下是否可以使用? 全班: 测试(在我的机器上编译没有问题,Java 1.7,但我没有使用<>所以它应该可以很好地与其他版本): Let's say I have: I'd like to be able to do the following: where Edit: It works! See my test added on the bottom. Would the following work? Whole class: The test (compiles without problems on my machine, Java 1.7 but I didn't use the <> so it should work fine with other versions): 这篇关于java泛型:接受一个类或接口的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!
类< ;?延伸T>
public class Components< T>扩展TupleList< Class<?extends T>,String> {
private static final long serialVersionUID = 1L;
public void add(Class classe,String name){
this.add(new Tuple< Class ,String>(classe,name)) ;
public interface Interface {}
public class Test< T>
{
public void add(Class< ;? extends T> x)
{
}
public static void x()
{
测试<接口> t = new Test< Interface>();
t.add(Implementation.class);
public static class实现实现Interface {}
}
public class Components<T> extends TupleList<Class<T>, String> {
private static final long serialVersionUID = 1L;
public void add(Class<T> classe, String name) {
this.add(new Tuple<Class<T>, String>(classe, name));
}
}
Components<IFilter> engines=new Components<IFilter>(){{
add(FilterEngineIdentity.class, "Identity");
}};
FilterEngineIdentity implements IFilter
. How would I achieve this without binding my class Components
to more specific class definitions?Class<? extends T>
public class Components<T> extends TupleList<Class<?extends T>, String> {
private static final long serialVersionUID = 1L;
public void add(Class<? extends T> classe, String name) {
this.add(new Tuple<Class<? extends T>, String>(classe, name));
}
}
public interface Interface {}
public class Test<T>
{
public void add(Class<? extends T> x)
{
}
public static void x()
{
Test<Interface> t = new Test<Interface>();
t.add(Implementation.class);
}
public static class Implementation implements Interface{}
}