简体   繁体   English

Hashtable是否实现Map接口中的所有方法?

[英]Does the Hashtable implement every method in the Map interface?

Since a class that implements an interface in Java must define every method in the interface to avoid being declared as abstract, I was wondering about the following: 由于用Java实现接口的类必须定义该接口中的每个方法,以避免被声明为抽象,所以我想知道以下内容:

When I create a program that instantiates a Hashtable object, why am I not required to define every method in the Map interface? 创建实例化Hashtable对象的程序时,为什么不需要在Map接口中定义每个方法? Are the methods I do not explicitly define, created automatically as "stubs"? 我没有明确定义的方法会自动创建为“存根”吗?

You are creating an object from Hashtable class which already implemented all necessary methods.You are not creating a class, but an object from that class. 您正在从Hashtable类创建一个对象,该对象已经实现了所有必需的方法。您不是在创建类,而是从该类创建对象。 If you created a class which implements Map , the compiler would ask you to implement all the necessary methods. 如果您创建了一个实现Map的类,则编译器会要求您实现所有必要的方法。

Does the Hashtable implement every method in the Map interface? Hashtable是否实现Map接口中的所有方法?

Yes. 是。 If you take a look at the JavaDoc the Hashtable class has been defined as 如果您看一下JavaDoc,则将Hashtable类定义为

public class Hashtable<K,V>
extends Dictionary<K,V>
implements Map<K,V>, Cloneable, Serializable

Notice, that the class has not been declared abstract and it implements Map . 请注意,这个类没有被声明为抽象的 ,它实现了Map。 Hence, it must and it does implement all the methods defined in Map interface. 因此,它必须并且确实实现了Map接口中定义的所有方法。

When I create a program that instantiates a Hashtable object, why am I not required to define every method in the Map interface? 创建实例化Hashtable对象的程序时,为什么不需要在Map接口中定义每个方法?

When you instantiate an object, the implementation of its methods is provided by its class. 实例化对象时,其方法的实现由其类提供。 So, when you instantiate a Hashtable object, it uses the implementation already provided by the Hastable class. 因此,当实例化Hashtable对象时,它使用Hastable类已经提供的实现。

The need for providing an implementation comes when you're creating a class not when instantiating an object from it. 当您创建类而不是实例化类时,需要提供实现。 At instantiation the class must not be abstract ie the implementation should already be present; 在实例化时,该类不能是抽象的,即实现应该已经存在。 either provided by you or like in the case of Hashtable by the JDK. 要么由您提供,要么就JDK的Hashtable

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

相关问题 Hashtable 实现了一个 set 接口,但 1 个测试不起作用 - Hashtable implement a set interface, but 1 test not working Map 接口是否扩展或实现了一些其他接口或 class? - Does Map interface extend or implement some other interface or class? 具有实现接口的对象的方法? - Method with objects that implement an interface? Java在实现接口中实现接口类型方法 - Java Implement an interface type method in implement interface 在未实现Cloneable接口的对象上调用clone()方法时,不会引发CloneNotSupportedException - CloneNotSupportedException not thrown on calling clone() method on object that does not implement Cloneable interface SQLiteDatabase&#39;没有实现接口&#39; - SQLiteDatabase 'does not implement interface' 如何记录一个接口的所有实现的每个方法的时间成本 - How record every method time cost of one interface‘s all implement 当父 class 未实现接口时,在父 class 中实现接口的方法有什么风险? - What are the risks of having the method implementation of an interface in a parent class when the parent class does not implement the interface? 必须实现默认接口方法吗? - Must implement default interface method? 具有扩展接口参数的实现方法 - Implement method with extended interface parameter
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM