简体   繁体   English

在单个java.lang.Object实例中合并多个对象

[英]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 ). 我有3个对象(让他们成为对象类A的,对象B B类和对象的C类的C),我需要将它们结合起来,java.lang.Object中的单个实例(让它是O)。 So in the end object o will contain inside it the three objects mentioned above. 因此,最终对象o将在其中包含上述三个对象。 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 : 对于getter,没有问题,您可以将当前数组返回给setter,可以使用Collections算法addAll作为示例

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

public void setArray(String[] sourceArray){

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

  Collections.addAll(list, values);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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