简体   繁体   English

为什么列出接口而不是类

[英]Why is List an Interface and not a Class

My doubt is between a simple class with implementations of methods and an interface. 我的疑问是在带有方法实现的简单类和接口之间。 not the abstract class. 不是抽象类。 Why cant we just make a List Class , give all the method implementations and make Array List or Linked list to extend it. 为什么我们不能仅仅创建一个List Class,给出所有方法的实现,并创建Array List或Linked list来扩展它。 arent the implementations of methods same. 相同方法的实现。

No, the implementations are not the same. 不,实现方式不同。 A linked list contains a collection of doubly linked nodes and is optimized for fast insertion/removal. 链表包含双向链接节点的集合,并针对快速插入/移除进行了优化。 An array list is backed by an array of references and is optimized for random access, but will be less efficient as elements are added and removed. 数组列表由引用数组支持,并且针对随机访问进行了优化,但是随着元素的添加和删除效率会降低。

Defining List as an interface allows callers to apply list-based semantics regardless of the internal details. List定义为接口允许调用者应用基于列表的语义,而无需考虑内部细节。

In Java, multi-inheritance is not possible. 在Java中,多重继承是不可能的。 So if a class extends another class, it cannot extend anything else. 因此,如果一个类extends另一个类,则它不能扩展其他任何东西。 Whereas implementing an interface is unrestrained. 而实现interface是不受限制的。 You can implement multiple interfaces with one class. 您可以使用一个类implement多个接口。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM