简体   繁体   English

java.util包 - 类和接口

[英]java.util package - classes vs interfaces

Why is Queue an interface, but others like Stack and ArrayList are classes? 为什么Queue是一个接口,但像StackArrayList这样的其他类是什么?

I understand that interfaces are made so that clients can implement them and add on their own methods, whereas with classes if every client needs their methods in there it will become huge and bloated.. 我理解接口是这样的,客户端可以实现它们并添加自己的方法,而对于类,如果每个客户端都需要它们的方法,它将变得庞大和膨胀。

...or am I missing something here? ......或者我在这里遗失了什么?

A Queue can be implemented in a number of fashions, as can a List or a Set . Queue可以以多种方式实现, ListSet也可以实现。 They all merely specify a contract for different kinds of collections. 它们都只是为不同类型的集合指定合同。

An ArrayList , however, is a particular implementation of a List , made to internally use an array for storing elements. 但是, ArrayListList的特定实现,用于在内部使用数组来存储元素。 LinkedList is also an implementation of a List , which uses a series of interconnected nodes, ie a doubly linked list . LinkedList也是List的实现,它使用一系列互连的节点,即双向链表 Similarly, TreeSet and HashMap are particular implementations of sets and maps, respectively. 类似地, TreeSetHashMap分别是集合和映射的特定实现。

Now, Stack is a odd case here, particularly because it is a legacy class from older versions of Java. 现在, Stack在这里是一个奇怪的例子,特别是因为它是旧版Java的遗留类。 You really shouldn't use a Stack anymore; 你真的不应该再使用Stack了; instead, you should use its modern equivalent, the ArrayDeque . 相反,你应该使用它的现代等价物, ArrayDeque ArrayDeque is an implementation of a Deque (a double-ended queue), that internally uses an array for storage (which is just what Stack does). ArrayDeque是一个Deque (一个双端队列)的实现,它在内部使用一个数组进行存储(这正是Stack所做的)。 A Deque supports all of the operations of a Stack , like pop , push , etc. Other implementations of Deque include LinkedList , as mentioned by someone else, although this deviates from Stack in that underlying it is not an array, but a doubly-linked list :-p Deque支持Stack所有操作,如poppushDeque其他实现包括LinkedList ,如其他人所提到的,尽管这与Stack ,底层不是数组,而是双重链接列表:-p

Now, there are plenty of implementations of Queue , and many different types of Queue s. 现在,有很多Queue的实现,以及许多不同类型的Queue You not only have BlockingQueue s (often used for producer-consumer), whose common implementations include LinkedBlockingQueue and ArrayBlockingQueue , but also TransferQueue s, and so on. 您不仅拥有BlockingQueue (通常用于生产者 - 消费者),其常见实现包括LinkedBlockingQueueArrayBlockingQueue ,还有TransferQueue ,等等。 I digress... you can read more on the collections API in the relevant Java Tutorial . 我离题了......您可以在相关的Java教程中阅读有关集合API的更多信息。

You get the idea of interfaces correctly. 您可以正确理解接口。 In this case Java standard library already provides both implementations and interfaces. 在这种情况下,Java标准库已经提供了实现和接口。 You are better of using an interface so you can switch the implementation any time. 您最好使用接口,以便随时切换实施。

Hope it makes sense. 希望它有意义。

I think Stack is well-renowned for being a class that should be an interface. 我认为Stack作为一个应该是接口的类而闻名。 The Java libraries are a bit hit-and-miss when it comes to correctly choosing to provide an interface. 在正确选择提供接口时,Java库有点昙花一现。

ArrayList is just an implementation of the List interface, so Sun got it correct there! ArrayList只是List接口的一个实现,所以Sun在那里得到了正确的答案! Another classic miss (in my opinion) is the Observable class, which very much needs to be the default implementation of an interface, rather than just a class. 另一个经典的错过(在我看来)是Observable类,它非常需要是接口的默认实现,而不仅仅是一个类。

Interesting question. 有趣的问题。 My thought about this that Queue is a basis for a lot of data structures like BlockingQueue, PriorityQueue, Deque, etc. This bunch of classes need specific implementation for various operations so it's much simpler to made Queue as interface. 我想到这一点, Queue是很多数据结构的基础,比如BlockingQueue,PriorityQueue,Deque等。这一类需要针对各种操作进行特定的实现,因此将Queue作为接口变得更加简单。

The reason interfaces are used for List and Queue is NOT to reduce excessive code. 接口用于List和Queue的原因不是减少过多的代码。

The main advantage of interfaces is they allow you to write flexible, loosely coupled code. 接口的主要优点是它们允许您编写灵活,松散耦合的代码。

(Here's an awesome answer that describes this concept perfectly) (这是一个很棒的答案 ,完美地描述了这个概念)

An interface simply defines a list of methods that will be implemented by a class. 接口只是定义一个将由类实现的方法列表。

This allows us to do a wonderfully powerful thing: 这让我们做了一件非常有力的事情:

  • We can treat all classes that implement an interface the same. 我们可以将实现接口的所有类都视为相同。

This is a HUGE advantage. 这是一个巨大的优势。

Here's a very simple example: 这是一个非常简单的例子:

We want to write a debug method that prints every element in a Collection . 我们想编写一个调试方法来打印Collection中的每个元素。

Collection is an interface. Collection是一个界面。 It defines a list of operations and does not implement them. 它定义了一个操作列表,但没有实现它们。

You cannot instantiate a Collection. 无法实例化集合。 You can instantiate a class that implements Collection. 可以实例化一个实现Collection的类。

There are many classes that implement Collection : ArrayList, Vector, TreeSet, LinkedList, etc... They all have different snazzy features, but they also have certain things in common: Because each class implements Collection , they all implement each method found here . 有许多类实现Collection :ArrayList,Vector,TreeSet,LinkedList等......它们都有不同的时髦特征,但它们有一些共同点:因为每个类都实现了Collection ,它们都实现了这里找到的每个方法。

This allows us to do a very powerful thing: 这使我们能够做一个非常强大的事情:

  • We can write a method that operates on ANY class that implements Collection . 我们可以编写一个在任何实现Collection的类上运行的方法

It would look just like this: 它看起来像这样:

public void printCollection(Collection semeCollection) {
    for (Object o : someCollection) {
       String s = (o == null) ? "null" : o.toString(); 
       System.out.println(s);
    }
}

Because of the magic of interfaces, we can now do the following: 由于接口的神奇之处,我们现在可以执行以下操作:

public void testStuff() {

    Collection s = new TreeSet();
    Collection a = new ArrayList();
    Collection v = new Vector();

    s.add("I am a set");
    a.add("I am an array list");
    v.add("I am a vector");

    printCollection(s);
    printCollection(a);
    printCollection(v);
}

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

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