java 定义'Set set = new HashSet()'时,是设置接口还是类Set的实例?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3715209/
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 03:08:06  来源:igfitidea点击:

When defining 'Set set = new HashSet()', is set an instance of interface or class Set?

javacollections

提问by Annibigi

In Java, 'Set' and 'List' are interfaces derived from 'Collection' interface. If we use the code:

在 Java 中,“Set”和“List”是从“Collection”接口派生的接口。如果我们使用代码:

import java.util.*;

public class SetExample{

    public stactic void main(String[] args){
      Set set = new HashSet();
      //do something .....
    }

}

Is there a class 'Set' in "Collection" API that we are creating an object ('set') of? or we are instantiating a interface 'Set'?

“集合”API 中是否有一个类“集合”,我们正在为其创建一个对象(“集合”)?或者我们正在实例化一个接口“Set”?

Am really confused.......:O

我真的很困惑.......:O

回答by Stephen C

java.util.Setis an interface, not a class. So

java.util.Set是一个接口,而不是一个类。所以

Set set = new HashSet();

creates an object that is a HashSetinstance, and assigns a reference to that object to a variable whose type is Set. This works because the HashSetclass implementsthe Setinterface. On the other hand:

创建一个HashSet实例对象,并将对该对象的引用分配给类型为 的变量Set。这是有效的,因为HashSet该类实现Set接口。另一方面:

Set set = new Set();

gives a compilation error because you cannot create an instance of an interface.

给出编译错误,因为您无法创建接口的实例。

An Java interface is essentially a contract between an implementation (a class) and the things that use it. It says what the names and signatures of a conforming object's methods are, but nothing about the object's state or how its methods work.

Java 接口本质上是实现(类)和使用它的事物之间的契约。它说明了符合对象的方法的名称和签名是什么,但没有说明对象的状态或其方法如何工作。

(Just to confuse things a bit ... Java also allows you to write something like this:

(只是为了混淆一些东西...... Java 也允许你写这样的东西:

Set set = new Set() {
    // attributes and methods go here
};

This is does not create an "instance" of the Setinterface per se... because that doesn't make sense. Rather, it declares and instantiates an anonymous classthat implementsthe Setinterface.)

这是不创建的“实例”Set界面本身......因为那是没有意义的。相反,它声明并实例化一个匿名类工具Set界面。)

回答by Sean Patrick Floyd

Here are some pointers:

以下是一些提示:

You should also read Effective Java by Joshua Bloch, especially item 52: "Refer to objects by their interfaces" (There's a small snippet viewable here)

您还应该阅读Joshua Bloch 的 Effective Java,尤其是第 52 条:“通过接口引用对象”(这里有一个小片段可以查看

回答by Balaji V

java.util.Setinterface provides the loose coupling with java.util.HashSetobject. So developer can use the java.util.Setreference for another java.util.Setinterface family object.

java.util.Set接口提供了与java.util.HashSet对象的松耦合。因此开发人员可以使用java.util.Set另一个java.util.Set接口族对象的引用。

回答by Rahul

The reference setis of type java.util.Setwhich is an interface. Although it actually points to an object of type java.util.HashSet. (polymorphic)

引用set的类型java.util.Setinterface. 虽然它实际上指向一个类型为 的对象java.util.HashSet。(多态)

回答by Gadolin

In API you get a bunch of interfaces that hides the implementation. e.g. Set allows you to hide any implementation, for which HashSet is one of.

在 API 中,你会得到一堆隐藏实现的接口。例如,Set 允许您隐藏任何实现,HashSet 就是其中之一。