gpt4 book ai didi

java - 从父类列表中提取子类元素

转载 作者:行者123 更新时间:2023-11-29 07:42:51 24 4
gpt4 key购买 nike

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

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);}
}

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

编辑:

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

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 的关键字,它将实例化对象与类定义进行比较,以查看该对象是否属于该特定类类型。

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

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

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

另外请注意,instanceof 关键字是 java 中的内置二元运算符,它将根据您之后指定的类检查父级。如果您可以将 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.

关于java - 从父类列表中提取子类元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28489322/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com