简体   繁体   中英

Merge multiple object in a single instance of java.lang.Object

I have 3 objects (let them be object a of class A, object b of class B and object c of class C) and I need to combine them to a single instance of java.lang.Object (let it be o ). So in the end object o will contain inside it the three objects mentioned above. Any ideas on how this one can be achieved?

Do you mean something like this?

public class Triplet<A, B, C> {
    private final A a;
    private final B b;
    private final C c;
    Triplet(A a, B b, C c){
        this.a = a;
        this.b = b;
        this.c = c;
    }
}

public class MyClassA {}
public class MyClassB {}
public class MyClassC {}

public class Main {
    public static void main(String[] args){
        Object o = new Triplet<>(new MyClassA(), new MyClassB(), new MyClassC());
    }
}

For getter , no issues you can return the current array for setter , you can use the Collections algorithm addAll for exemple :

String[] values = { "cat", "dog", "bird" };
setArray(values);

public void setArray(String[] sourceArray){

  ArrayList<String> list = new ArrayList<>();
  list.add("elephant");

  Collections.addAll(list, values);
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM