在抽象类集合中查找继承项

发布于 2024-10-27 02:11:18 字数 508 浏览 1 评论 0原文

我有以下类:

public class CollectionCustomClass extends ArrayList<CustomClass>
public abstract class CustomClass
public class SubClass1 extends CustomClass
public class SubClass2 extends CustomClass

并且在一个方法中我想要执行以下操作:

CollectionCustomClass ccc = new CollectionCustomClass();
ccc.add(new SubClass1())
ccc.add(new SubClass2())
ccc.add(new SubClass1())
ccc.add(new SubClass2())

ccc.find(SubClass1)

结果将是 2 Subclass1。

我怎样才能做到这一点?

I have the following classes:

public class CollectionCustomClass extends ArrayList<CustomClass>
public abstract class CustomClass
public class SubClass1 extends CustomClass
public class SubClass2 extends CustomClass

and in a method i want to do the following:

CollectionCustomClass ccc = new CollectionCustomClass();
ccc.add(new SubClass1())
ccc.add(new SubClass2())
ccc.add(new SubClass1())
ccc.add(new SubClass2())

ccc.find(SubClass1)

The result would be the 2 Subclass1.

How can i achieve this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

君勿笑 2024-11-03 02:11:18

尝试

 ccc.find(SubClass1.class);

 class CollectionCustomClass<T> extends ArrayList<CustomClass>{

    public CustomClass find(Class<T> clazz) {
        for(int i=0; i< this.size(); i++)
        {
            CustomClass obj = get(i);
            if(obj.getClass() == clazz)
            {
                return obj;
            }
        }
        return null;
    }
 }

Try

 ccc.find(SubClass1.class);

and

 class CollectionCustomClass<T> extends ArrayList<CustomClass>{

    public CustomClass find(Class<T> clazz) {
        for(int i=0; i< this.size(); i++)
        {
            CustomClass obj = get(i);
            if(obj.getClass() == clazz)
            {
                return obj;
            }
        }
        return null;
    }
 }
独留℉清风醉 2024-11-03 02:11:18

如果您想要确切类别的项目,只需对每个项目调用 getClass 并与您想要的类别进行比较。

If you want items of the exact class, simply call getClass on each item and compare with the class that you want.

智商已欠费 2024-11-03 02:11:18

如果我理解正确,您可以迭代 ArrayList 并进行以下比较:

if( listName.get(i).getClass() == passedClass ){ //increase count for this class }

If I understand you correctly, you can iterate through the ArrayList and do the following compare:

if( listName.get(i).getClass() == passedClass ){ //increase count for this class }
稚气少女 2024-11-03 02:11:18

ArrayList 不包含 .find(Class) 方法。

http://download.oracle.com/javase/6 /docs/api/java/util/ArrayList.html

您需要在 CollectionCustomClass 上实现该方法。

在伪代码中,它会是这样的:

public List CollectionCustomClass.find(CustomClassclazz) {
  List<CustomClass> out = new ArrayList<CustomClass>();

  // Loop through list and use instanceof to add items to out

  return out;

}

您还可以将泛型应用于此方法。

ArrayList does not contain a .find(Class) method.

http://download.oracle.com/javase/6/docs/api/java/util/ArrayList.html

You would need to implement that method on CollectionCustomClass.

In pseudo-code it would be something like this:

public List CollectionCustomClass.find(CustomClassclazz) {
  List<CustomClass> out = new ArrayList<CustomClass>();

  // Loop through list and use instanceof to add items to out

  return out;

}

You could also apply generics to this method.

Oo萌小芽oO 2024-11-03 02:11:18

你可以在你的集合类中有 find 方法,如下所示

public int find(String className) {
        int count = 0;
        for(int i=0; i<this.size();i++) {
            if(className == this.get(i).getClass().getName()) {
                count++;
            }
        }
        return count;
    }

you can have find method in your collection class like this

public int find(String className) {
        int count = 0;
        for(int i=0; i<this.size();i++) {
            if(className == this.get(i).getClass().getName()) {
                count++;
            }
        }
        return count;
    }
素食主义者 2024-11-03 02:11:18
public <T> CollectionCustomClass find(Class<T> clazz) {
    CollectionCustomClass answer = new CollectionCustomClass();
    for (Entity entity : this) {
        if (entity.getClass() == clazz) {
            answer.add(entity);
        }
    }
    return answer;
}
public <T> CollectionCustomClass find(Class<T> clazz) {
    CollectionCustomClass answer = new CollectionCustomClass();
    for (Entity entity : this) {
        if (entity.getClass() == clazz) {
            answer.add(entity);
        }
    }
    return answer;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文