从父列表 class 中提取子 class 元素

Extract subclass elements from List of parent class

我目前有一个这样的列表:

List<Parent> parentList = new Arraylist<>;
//Code inserts elements of subclassA and subclass B into parentList above

现在我想从 parentList 中提取子类 A 的元素。我想执行以下操作但没有错误:

List<SubclassA> extractMe = new Arraylist<>;
for (Parent parent: parentList){
    if (parent.isSubclassA()){extractMe.add(parent);}
}

我不确定我该怎么做。我不断收到错误消息,提示我将可能是错误的对象 type/class 添加到列表中,我理解原因,但我不知道如何更改代码以使其正常工作。

编辑:

错误(我已重命名 类 以匹配上面使用的错误):

Error:(53, 20) java: no suitable method found for add(Parents.Parent)
        method java.util.Collection.add(Parents.SubclassA) is not applicable
          (argument mismatch; Parents.Parent cannot be converted to Parents.SubclassA)
        method java.util.List.add(Parents.SubclassA) is not applicable
          (argument mismatch; Parents.Parent cannot be converted to Parents.SubclassA)

java 中有一个名为 instanceof 的关键字,它将实例化对象与 class 定义进行比较,以查看该对象是否属于特定的 class 类型。

在您的示例中,您可以在循环中使用 instanceof 关键字,然后将父对象转换为子对象class并将其添加到您的列表中。

for(Parent parent : parentList)
{
    if(parent instanceof SubclassA)
    {
        SubclassA subclass = (SubclassA)parent;
        extractMe.add(subclass);
    }
}

需要将父对象转换为子对象class (SubclassA)parent,因为即使 you 可能已经检查过 parent 的类型SubclassA 编译器仍然不知道 ,因此您必须明确告诉编译器将 parent 对象转换为 SubclassA

另外请注意,instanceof 关键字是 java 中的内置二元运算符,它将根据您之后指定的 class 检查父级。如果您可以将 parent 转换为 SubclassAparent instanceof SubclassA 只有 为真。见 Oracle Docs Example:

The Type Comparison Operator instanceof

The instanceof operator compares an object to a specified type. You can use it to test if an object is an instance of a class, an instance of a subclass, or an instance of a class that implements a particular interface.

The following program, InstanceofDemo, defines a parent class (named Parent), a simple interface (named MyInterface), and a child class (named Child) that inherits from the parent and implements the interface.

class InstanceofDemo {
    public static void main(String[] args) {

        Parent obj1 = new Parent();
        Parent obj2 = new Child();

        System.out.println("obj1 instanceof Parent: "
            + (obj1 instanceof Parent));
        System.out.println("obj1 instanceof Child: "
            + (obj1 instanceof Child));
        System.out.println("obj1 instanceof MyInterface: "
            + (obj1 instanceof MyInterface));
        System.out.println("obj2 instanceof Parent: "
            + (obj2 instanceof Parent));
        System.out.println("obj2 instanceof Child: "
            + (obj2 instanceof Child));
        System.out.println("obj2 instanceof MyInterface: "
            + (obj2 instanceof MyInterface));
    }
}

class Parent {}
class Child extends Parent implements MyInterface {}
interface MyInterface {}

Output:

obj1 instanceof Parent: true
obj1 instanceof Child: false
obj1 instanceof MyInterface: false
obj2 instanceof Parent: true
obj2 instanceof Child: true
obj2 instanceof MyInterface: true

When using the instanceof operator, keep in mind that null is not an instance of anything.