简体   繁体   English

Java不同类型的对象作为相同的参数

[英]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.

相关问题 在Java中的不同类中使用相同类型的两个不同对象 - Using two different objects of the same type in different classes in Java Java 不同参数类型(数字)但算法相同的过载 - Java overload on different parameter type (numeric) but same algorithm Java:将两个具有相同值的不同类型的对象进行比较,返回true - Java: comparing two Objects of different type with same value, returning true 具有相同数据Java的不同对象 - Different objects with the same data java 使用不同对象作为参数调用的相同方法 - Same method called with different objects as parameter 如果不同的Java泛型类型参数具有相同的名称,为什么可以为其分配不同的类型? - If different Java generic type parameter have the same name, why it can be assigned different types? 在Java的Collections对象中两次声明类型参数 - Declaring type parameter twice in Collections objects in Java 使用EnumSet(不同类型)作为参数的相同方法名称 - Same method name with EnumSet (of different type) as a parameter Java:根据参数类型调用所有相同的超类的最有效方法 - Java: Most efficient way of calling different methods depending on the parameter type, which all share the same superclass 在同一TCP套接字上接收不同类型的对象 - Recive different type of objects on the same TCP socket
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM