java 扩展 ArrayList 并创建新方法

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6524711/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 16:11:52  来源:igfitidea点击:

Extending ArrayList and Creating new methods

javaarraylistextend

提问by Cody

I'm having a bit of a problem grasping something - I might be going about this completely wrong.

我在抓住某些东西时遇到了一些问题 - 我可能完全错误地处理了这个问题。

I am trying to create a class which extends ArrayList but has several methods which increase the functionality (at least for the program I am developing.)

我正在尝试创建一个扩展 ArrayList 的类,但有几种方法可以增加功能(至少对于我正在开发的程序而言)。

One of the methods is a findById(int id), which searches each ArrayList object for a particular id match. So far it's working, but it won't let me do for (Item i : this) { i.getId(); }

其中一种方法是 findById(int id),它在每个 ArrayList 对象中搜索特定的 id 匹配项。到目前为止它的工作,但它不会让我做for (Item i : this) { i.getId(); }

I don't understand why?

我不明白为什么?

Full code:

完整代码:

public class CustomArrayList<Item> extends ArrayList<Item> {

    // declare singleton instance
    protected static CustomArrayList instance;

    // private constructor
    private CustomArrayList(){
        // do nothing
    }

    // get instance of class - singleton
    public static CustomArrayList getInstance(){
        if (instance == null){
            instance = new CustomArrayList();
        }
        return instance;
    }

    public Item findById(int id){
        Item item = null;
        for (Item i : this) {
            if (i.getId() == id) {
                      // something
         }
        }
        return item;
    }
    public void printList(){
        String print = "";
        for (Item i : this) {
            print += i.toString() + "\n";
        }
        System.out.println(print);
    }
}

回答by aioobe

Change

改变

public class CustomArrayList<Item> extends ArrayList<Item> {

to

public class CustomArrayList extends ArrayList<Item> {


I suspect Itemis the name of the class that you want to store in the list. By adding <Item>after CustomArrayListyou're introducing a type parameterwhich shadows this class.

我怀疑Item是您要存储在列表中的类的名称。通过在引入隐藏此类的类型参数<Item>之后添加。CustomArrayList



Withthe <Item>parameter, your code is equal to

随着<Item>参数,你的代码是等于

public class CustomArrayList<T> extends ArrayList<T> {
    // ...
        for (T i : this) { i.getId(); }
    // ...
}

which obviously won't always work, as Tmay refer to any type.

这显然并不总是有效,因为T可能指的是任何类型。

回答by Jon Skeet

What is getId()? Presumably it's a method in someclass, but we don't know whichclass.

什么是getId()?大概是某个类中的一个方法,但是我们不知道是哪个类。

If you've actually got a class called Itemwith a getId()method, which this is meant to be a list of, you simply need to stop your class from being generic. So instead of this:

如果你真的有一个ItemgetId()方法调用的类,这意味着它是一个列表,你只需要阻止你的类成为泛型。所以而不是这个:

public class CustomArrayList<Item> extends ArrayList<Item> {

you want:

你要:

public class CustomArrayList extends ArrayList<Item> {

Currently within your class, Itemdoesn't refer to a class called Item, it refers to a type parametercalled Item.

目前你的类中,Item并非指一类叫做项目,它是指一个类型的参数,称为Item

Now personally:

现在个人:

  • I wouldn't avoid creating singletons unless you really have to
  • If you have to, I'd avoid creating them in the way you have (which isn't thread-safe)
  • I wouldn't extend ArrayList<>unless I really had to, preferring composition over inheritance
  • 我不会避免创建单身人士,除非你真的必须这样做
  • 如果必须,我会避免以您拥有的方式创建它们(这不是线程安全的)
  • ArrayList<>除非我真的必须,否则我不会扩展,更喜欢组合而不是继承