我有一个分配,我可以创建一个双向散列表。 我倾向于为bidirectionalHashTable创建一个构造函数,但是我完全不知道如何创建一个,因为我通常不使用它。 任何人都可以给我一个关于我要做什么的小费?我喜欢使用构造函数来存储我可以用hashTable做的所有可能的操作吗?
以下是代码:
public class BidirectionalHashtable<K, V> {
/**
* construtor
*/
public BidirectionalHashtable() {
}
/**
* returns a value, gets the corresponding key
* @param value - the value
* @return K - the key
*/
public K getKey(V value) {
return null;
}
/**
* receives a key, gets the corresponding value
* @param key - key
* @return V - value
*/
public V getValue(K key) {
return null;
}
以及更多方法,如put(),containsValue()等等
答案 0 :(得分:0)
我的建议?删除构造函数。构造函数设置对象的状态(字段),这可能是正确功能所必需的。通常,没有什么特别需要 - 需要初始化的字段可以作为其声明的一部分给出值,例如:
private String prefix = ""; // declare an initial value
private String name; // if not initialized, it's null
如果你有代码,你必须在构造函数中运行声明一个 如果您不需要任何此类代码,请不要定义(空)代码。