实际上,我想创建一个能够处理多种类型的 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 - 是否可以将类 <protocol> 作为参数传递?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16978126/