你能用Java扩展ArrayList吗?
是否可以创建一个扩展 ArrayList 的子类?如果是这样,怎么办?
Is it possible to make a child class that extends ArrayList? If so, how?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
是否可以创建一个扩展 ArrayList 的子类?如果是这样,怎么办?
Is it possible to make a child class that extends ArrayList? If so, how?
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
接受
或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
发布评论
评论(6)
您可以扩展 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
是的,你可以。
但是,我不确定您为什么要这样做。
Yes you can.
However, I'm not sure why you would want to do this.
正如许多其他人所说,是的,您可以扩展类 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 classPersonList
that extends the C# equivalent ofArrayList<Person>
.The common idiom in Java would just to use
ArrayList<Person>
if you need a list ofPerson
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.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
试试吧。该类不是最终的,它的构造函数是公共的,所以你可以。然而,对于初学者来说这可能不是一个好主意。
大多数时候,这对任何人来说都不是一个好主意。想象一下,您添加了一些功能并获得
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 haveExtList2 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.正如其他人所说,扩展 java lang 数据结构是一个非常糟糕的主意。
但是,如果您想在集合类中隔离一些逻辑,我建议使用以下解决方案:
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: