[英]Java different type objects as same parameter
I am currently writing an function for manipulate several ArrayList. 我目前正在编写用于操纵多个ArrayList的函数。
All the elements have same constructor, how can I do it like below? 所有元素都有相同的构造函数,我怎么能像下面这样做?
Class A{
public A (Cursor data){...}
}
Class B{
public B (Cursor data){...}
}
Class C{
public C (Cursor data){...}
}
public void dataManipulation(ArrayList<**?**> list, Cursor cursor){
cursor.moveToFirst();
for (int i = 0; i < cursor.getCount(); i++) {
list(new **?**(cursor));
if (!cursor.isAfterLast())
cursor.moveToNext();
}
public void main(){
ArrayList<A> listA = new ArrayList<A>();
ArrayList<B> listB = new ArrayList<B>();
ArrayList<C> listC = new ArrayList<C>();
Cursor cursor = IMPORT_CURSOR_FROM_FILE;
dataManipulation(listA, cursor);
dataManipulation(listB, cursor);
dataManipulation(listC, cursor);
}
Start by taking a look at Interfaces and Inheritance , basically an interface is a contract that gurentees that any object that implements that interface will provide the contractual functionality... 首先看一下接口和继承 ,基本上一个接口是一个合同,它可以实现任何实现该接口的对象将提供合同功能......
For example... 例如...
public interface CursorContainer {
public Cursor getCursor();
}
You "common" class would implement this interface and provide implementations for the getCursor
(and any other required) method... 您的“普通”类将实现此接口,并提供
getCursor
(以及任何其他必需的)方法的实现...
Class A implements CursorContainer {
public A (Cursor data){...}
}
Class B implements CursorContainer {
public B (Cursor data){...}
}
Class C implements CursorContainer {
public C (Cursor data){...}
}
Then you could use generics... 然后您可以使用泛型...
public void dataManipulation(ArrayList<CursorContainer> list, Cursor cursor){
Your next problem is know how to create a particular implementation, for this, you will need a factory... 你的下一个问题是知道如何创建一个特定的实现,为此,你需要一个工厂......
public interface CursorContainerFactory {
public CursorContainer create(Cursor cursor);
}
You would need a factory for each type of container you want to create... 对于要创建的每种类型的容器,您都需要一个工厂......
public Class CursorContainerAFactory implements CursorContainerFactory {
public CursorContainer create(Cursor cursor) {
return new A(cursor);
}
}
You would then need to supply the factory to your dataManipulation
method... 然后,您需要为工厂提供
dataManipulation
方法。
public void dataManipulation(ArrayList<CursorContainer> list, CursorContainerFactory factory, Cursor cursor){
cursor.moveToFirst();
for (int i = 0; i < cursor.getCount(); i++) {
list(factory.create(cursor));
if (!cursor.isAfterLast())
cursor.moveToNext();
}
And finally, call it... 最后,称之为......
dataManipulation(listA, new CursorContainerAFactory(), cursor);
Remember, classes can implement many interfaces, but only extend from one... 请记住,类可以实现许多接口,但只能从一个扩展...
You would need to create an interface for each of your classes (A, B, C) to implement. 您需要为每个类(A,B,C)创建一个接口来实现。
See this: https://docs.oracle.com/javase/tutorial/java/concepts/interface.html 参见以下内容: https : //docs.oracle.com/javase/tutorial/java/concepts/interface.html
You might use reflection, but you'll need to pass in the Class
because of type-erasure. 您可能使用了反射,但是由于类型擦除,您需要传递
Class
。 Something like, 就像是,
public <T> void dataManipulation(Class<T> cls, ArrayList<T> list,
Cursor cursor) {
cursor.moveToFirst();
for (int i = 0; i < cursor.getCount(); i++) {
Constructor<T>[] ctors = (Constructor<T>[]) cls.getConstructors();
for (Constructor<T> c : ctors) {
Type[] types = c.getGenericParameterTypes();
if (types.length == 1) {
if (types[0].getClass().equals(Cursor.class)) {
list.add(c.newInstance(cursor));
break;
}
}
}
if (!cursor.isAfterLast()) {
cursor.moveToNext();
}
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.