-1

我是 Java 新手。我正在使用 Eclipse IDE。如果@override 存在而不是@override 已被删除,我会收到错误消息。任何人都可以在这方面帮助我。谢谢

HashDictionary 类型的方法 remove(K) 必须覆盖超类。这是我得到的错误。

public interface DictionaryInterface <K extends Comparable<K>, V>
{
    public void insert(K key, V value);
    public V getValue(K str);
    public void remove(K key);
    public K[] getKeys();   
}


public class HashDictionary <K extends Comparable<K>, V> implements DictionaryInterface <K,V>{ 
    Hashtable<K,V> h;
    Class<K> KeyType;
    HashDictionary(Class<K> KeyType) {
        h=new Hashtable<K, V>();
        this.KeyType=KeyType;
    }
    public K[] getKeys() {
        // TODO Auto-generated method stub
        Set<K> s = h.keySet();
        K elements[]=(K[])Array.newInstance(KeyType, h.size());
        int i=0;
        for(K e:s)
        {
            elements[i]=e;
            i++;
        }
        return elements;
}

    @Override
    public void remove(K key) {
        h.remove(key);
    }
    @Override
    public V getValue(K str) {
        return h.get(str);
    }
    @Override
    public void insert(K key, V value) {
        h.put(key, value);
    }
}
4

4 回答 4

3

问题是您的界面中DictionaryInterface没有方法 remove 或具有不同的签名。

如果删除注释 @Override ,则将其视为本地方法。

显示DictionaryInterface或交叉检查它。

更新:

你可能会在你的IDE. 在覆盖了所有方法之后,

public class HashDictionary <K extends Comparable<K>, V> implements DictionaryInterface <K,V>{ 
        Hashtable<K,V> h;
        Class<K> KeyType;
        HashDictionary(Class<K> KeyType) {
            h=new Hashtable<K, V>();
            this.KeyType=KeyType;
        }

          @Override
            public void remove(K key) {
                // TODO Auto-generated method stub
                h.remove(key);
            }

        @Override
        public void insert(K key, V value) {
            // TODO Auto-generated method stub

        }

        @Override
        public V getValue(K str) {
            // TODO Auto-generated method stub
            return null;
        }

        @Override
        public K[] getKeys() {
            // TODO Auto-generated method stub
            return null;
        }


    } 
于 2013-10-04T10:09:48.407 回答
0

我想如果您的 DictionaryInterface 是一种 Hashtable 或 HashMap,它remove可以假定Object为参数,而不是K.

于 2013-10-04T10:10:31.753 回答
0

您只能注释界面中的方法。看来你DictionaryInterface还没有一个方法叫public void remove(K key)

于 2013-10-04T10:11:58.613 回答
0

您发布的代码完美无缺。它没有错。我在Ideone对其进行了测试。在线没有编译错误。我添加abstract是为了能够编译而不必实现其他三种方法。

interface DictionaryInterface <K extends Comparable<K>, V>
{
    public void insert(K key, V value);
    public V getValue(K str);
    public void remove(K key);
    public K[] getKeys();   
}


abstract class HashDictionary <K extends Comparable<K>, V> implements DictionaryInterface <K,V>{ 
    Hashtable<K,V> h;
    Class<K> KeyType;
    HashDictionary(Class<K> KeyType) {
        h=new Hashtable<K, V>();
        this.KeyType=KeyType;
    }



    @Override
    public void remove(K key) {
        // TODO Auto-generated method stub
        h.remove(key);
    }
}
于 2013-10-04T10:18:12.320 回答