实际上,我想创建一个能够处理多种类型的ArrayList的函数。
public void someFunction(Class ArrayList<protocol>) {
ArrayList<?> object = new ArrayList<protocol>;
}
这样的话,有什么建议吗?
最佳答案
可能对你有帮助
public class TestClass {
public void function(List<? extends Object> temp){
ArrayList<? extends Object> obj=(ArrayList<? extends Object>) temp;
}
}
然后,您可以使用不同类型的参数以这种方式调用此函数。
TestClass test= new TestClass();
test.function(new ArrayList<String>());
test.function(new ArrayList<Integer>());
test.function(new ArrayList<TestClass>());
关于android - 是否可以通过class <protocol>作为参数?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/16978126/