Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/387.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 存储对不同类的对象的引用_Java_Arrays_Oop_Object - Fatal编程技术网

Java 存储对不同类的对象的引用

Java 存储对不同类的对象的引用,java,arrays,oop,object,Java,Arrays,Oop,Object,我目前有两个类,我从中创建对象。我需要一个数组来存储指向这些对象的引用(指针)。数组应该是什么类型的 ArrayList<Class1> visableObjs = new ArrayList<Class1>(); ArrayList visableObjs=new ArrayList(); 当然,将只存储指向源自Class1的对象的指针。如果我有一个来自Class2的对象,我可以将它的指针存储在同一个数组中吗?我们可以这样做。如果对象来自不同的类,这根本不是好的做法

我目前有两个类,我从中创建对象。我需要一个数组来存储指向这些对象的引用(指针)。数组应该是什么类型的

ArrayList<Class1> visableObjs = new ArrayList<Class1>();
ArrayList visableObjs=new ArrayList();

当然,将只存储指向源自Class1的对象的指针。如果我有一个来自Class2的对象,我可以将它的指针存储在同一个数组中吗?

我们可以这样做。如果对象来自不同的类,这根本不是好的做法

ArrayList<Object> visableObjs = new ArrayList<Object>();

如果您的意思是存储的对象是这两个类的实例,则应使这些类从(自定义?)类或接口继承,并使用该类/接口作为要存储在数组中的类型。

您可以使用泛型创建一个选项类来保存对一个或其他类型的引用,但不能同时使用这两个类型:

public final class Choice<A,B> {

    private final A a;
    private final B b;
    private final boolean isA;

    private Choice(A a, B b, boolean isA) {
        this.a = a; this.b = b; this.isA = isA;
    }

    public static <A,B> Choice<A,B> ofA(A a) {
        return new Choice<>(a, null, true);
    }
    public static <A,B> Choice<A,B> ofB(B b) {
        return new Choice<>(null, b, false);
    }

    public boolean isA() { return isA; }
    public A getA() {
       if(!isA) throw new IllegalStateException("Not a!");
       return a;
    }

    public boolean isB() { return !isA; }
    public B getB() {
       if(isA) throw new IllegalStateException("Not b!");
       return b;
    }

    // Purely for demo purposes...
    public static void main(String[] args) {
        Choice<Integer,String> ich = Choice.ofA(42);
        Choice<Integer,String> sch = Choice.ofB("foo");
        // This is why the isA is included; so we can tell a null A from a null B.
        Choice<Integer,String> nil = Choice.ofA(null);
        //
        List<Choice<Boolean,String>> xs = new ArrayList<Choice<Boolean,String>>();
        xs.add(Choice.ofA(true));
        xs.add(Choice.ofB("neep"));
    }

}
公共期末课程选择{
私人决赛A;
私人决赛B;
私有最终布尔isA;
私人选择(A、B、B){
this.a=a;this.b=b;this.isA=isA;
}
公共静态选择A(A){
返回新选项(a,null,true);
}
公共静态选择B(B){
返回新选项(null、b、false);
}
公共布尔值isA(){return isA;}
公共A getA(){
如果(!isA)抛出新的IllegalStateException(“不是一个!”);
返回a;
}
公共布尔值isB(){return!isA;}
公共B getB(){
如果(isA)抛出新的非法状态异常(“不是b!”);
返回b;
}
//纯粹为了演示的目的。。。
公共静态void main(字符串[]args){
Choice ich=Choice.ofA(42);
Choice sch=Choice.ofB(“foo”);
//这就是包含isA的原因;因此我们可以区分空a和空B。
选项nil=选项ofA(空);
//
List xs=new ArrayList();
xs.add(Choice.ofA(true));
xs.add(选择ofB(“neep”);
}
}
这应该适用于两个不相关的类。或者对于许多相关子类中的两个,您只想限制这两种可能性,而不是更一般的类/接口的任何子类

这样一个类可能应该被扩展以正确实现equals()/hashCode()、toString()等(对于“正确”的某些定义[有文档记录])


警告:这可能不是第一次编译-我手头没有javac来测试它。但是想法应该很清楚。

Class1实现了myClass,Class2实现了myClass,ArrayList请使用Java命名约定。你不应该让你的两个类从一个通用的超类(或接口)继承,除非它们真的是这个超类型的实例(在有意义的意义上)。你当然不应该只是为了把它们放在同一个收藏中。如果这样做,则会将类型与聚合异构集合中元素的需要混为一谈。可以让两个类实现一个接口,或者让两个类共享一个公共父类。然后,您可以将列表的类型设置为接口或公共父级的类型。这对你所做的事情最有意义。顺便说一句。这个想法来自Scala类:
public final class Choice<A,B> {

    private final A a;
    private final B b;
    private final boolean isA;

    private Choice(A a, B b, boolean isA) {
        this.a = a; this.b = b; this.isA = isA;
    }

    public static <A,B> Choice<A,B> ofA(A a) {
        return new Choice<>(a, null, true);
    }
    public static <A,B> Choice<A,B> ofB(B b) {
        return new Choice<>(null, b, false);
    }

    public boolean isA() { return isA; }
    public A getA() {
       if(!isA) throw new IllegalStateException("Not a!");
       return a;
    }

    public boolean isB() { return !isA; }
    public B getB() {
       if(isA) throw new IllegalStateException("Not b!");
       return b;
    }

    // Purely for demo purposes...
    public static void main(String[] args) {
        Choice<Integer,String> ich = Choice.ofA(42);
        Choice<Integer,String> sch = Choice.ofB("foo");
        // This is why the isA is included; so we can tell a null A from a null B.
        Choice<Integer,String> nil = Choice.ofA(null);
        //
        List<Choice<Boolean,String>> xs = new ArrayList<Choice<Boolean,String>>();
        xs.add(Choice.ofA(true));
        xs.add(Choice.ofB("neep"));
    }

}