你能用Java扩展ArrayList吗?

发布于 2024-10-13 10:58:29 字数 42 浏览 5 评论 0原文

是否可以创建一个扩展 ArrayList 的子类?如果是这样,怎么办?

Is it possible to make a child class that extends ArrayList? If so, how?

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

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

发布评论

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

评论(6

海风掠过北极光 2024-10-20 10:58:29

您可以扩展 Java 中任何非 final 的类。话虽如此,如果没有真正的 is-a 关系,您应该避免继承。考虑组合以便重用。了解里氏替换原理

You can extend any class that is not final in Java. Having said that, you should avoid inheritance if there is no true is-a relationship. Consider composition for reuse. Read about Liskov substitution principle

月亮邮递员 2024-10-20 10:58:29

是的,你可以。

public class MyArrayList<E> extends ArrayList<E>
{
}

但是,我不确定您为什么要这样做。

Yes you can.

public class MyArrayList<E> extends ArrayList<E>
{
}

However, I'm not sure why you would want to do this.

愁以何悠 2024-10-20 10:58:29

正如许多其他人所说,是的,您可以扩展类 ArrayList,但这不是您通常应该做的事情;这在 Java 中不被认为是好的实践。

我主要是一名 Java 程序员,但过去几个月我也一直在研究 C# 代码。如果您需要特定类型的集合,那么扩展标准集合类似乎是 C# 中的常见习惯用法(我实际上不知道这是否是一般的常见习惯用法 - 至少是编写我的代码的人)我的同事一直在这样做)。

因此,如果他们有一个 Person 类,并且需要一个人员列表,他们会创建一个 PersonList 类,该类扩展了 ArrayList< 的 C# 等效项。 /代码>。

如果您需要 Person 对象的列表,Java 中的常见习惯用法只是使用 ArrayList,而不是为此创建特定的子类。

我建议您坚持使用常见的 Java 处理方式,而不是创建您自己的 ArrayList 或其他集合类的子类。

As many other have said, yes, you can extend class ArrayList, but it is not something that you should normally do; it is not considered good practice in Java.

I'm mainly a Java programmer, but the past months I've also been working on C# code. It seems like it's a common idiom in C# to extend the standard collection classes if you need a collection of a specific type (I actually don't know if it is a common idiom in general - at least the people who wrote the code I'm working with are doing this all the time).

So if they have a class Person and they need a list of persons, they'd create a class PersonList that extends the C# equivalent of ArrayList<Person>.

The common idiom in Java would just to use ArrayList<Person> if you need a list of Person objects and not to create a specific subclass for this.

I'd advise you to stick to the common Java way of doing things, and not create your own subclasses of ArrayList or other collection classes.

将军与妓 2024-10-20 10:58:29

ArrayList不是最终类,它提供公共构造函数,因此从技术上讲它可以扩展。

但最佳实践是委托而不是扩展。
请参阅:装饰器模式

ArrayList is not final class and it provides public constructor, so technically it can be extended.

But best practice is delegate rather than extend.
See: Decorator pattern

夏有森光若流苏 2024-10-20 10:58:29

试试吧。该类不是最终的,它的构造函数是公共的,所以你可以。然而,对于初学者来说这可能不是一个好主意。

大多数时候,这对任何人来说都不是一个好主意。想象一下,您添加了一些功能并获得ExtList1 extends ArrayList。您的大学添加了不同的独立功能,因此您有 ExtList2 扩展了 ArrayList。现在你同时想要它们,那么你就不走运了。

或者您需要具有不同基本列表实现的相同功能(可能是 LinkedList,尽管使用它实际上总是错误的)。再次,运气不佳。

这些都是代表团获胜的情况。当 有人已经创建了基地


如果有充分的理由这样做,我只会继承 ArrayList。也许有一些基于适当的 JMH 基准的非常极端的性能要求。

Just try it out. The class is not final, it's constructor is public, so you can. However, it's probably no good idea for a beginner.

Most of the time, it's no good idea for anyone. Imagine you add some functionality and get ExtList1 extends ArrayList. A college of yours adds a different independent functionality, so you have ExtList2 extends ArrayList. Now you want them both at once and you're out of luck.

Or you need the same feature with a different base list implementation (maybe LinkedList, though it's virtually always wrong to use it). Again, out of luck.

These are all cases when delegation wins. It needn't be more verbose when someone has created the base already.


I'd only inherit from ArrayList, if there was a very good reason for doing exactly this. Maybe some really extreme performance requirements based on proper JMH benchmarks.

瞎闹 2024-10-20 10:58:29

正如其他人所说,扩展 java lang 数据结构是一个非常糟糕的主意。
但是,如果您想在集合类中隔离一些逻辑,我建议使用以下解决方案:

public class ProductCollection{

    ArrayList<Product> products;

    public Product getByCode(String code) {
       // ... your logic goes here.
    }
}

As others said, extending java lang data structures is a very bad idea.
However, if you have some logic you want to isolate in a collection class, I would suggest bellow solution:

public class ProductCollection{

    ArrayList<Product> products;

    public Product getByCode(String code) {
       // ... your logic goes here.
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文