2013-02-27 132 views
0

我在修改的Java日誌的一個相當基本的鏈表實現使用泛型工作。我的項目有3個.java文件:Java的編譯時錯誤:編譯器不能識別方法重寫

  • GenericLogInterface.java - >接口定義日誌
  • LLGenericNode.java - >類定義LL的節點
  • LinkedGenericLog.java - >接口的擴展程序,可限定從LinkedGenericLog.java的LL

不幸的是,我收到一個編譯時間錯誤:

ch02/genericStringLogs/LinkedGenericLog.java:10: ch02.genericStringLogs.LinkedGenericLog is not abstract and does not override abstract method contains(java.lang.Object) in ch02.genericStringLogs.GenericLogInterface

這似乎通過重寫從GenericLogInterface.java在LinkedGenericLog.java的包含()的方法容易地得到解決。

有一個巨大的掛斷,但是: 我已經重寫了。

下面是三個.java文件來源:

GenericLogInterface.java

package ch02.genericStringLogs; 

public interface GenericLogInterface<T> 
{ 
    void insert(T element); 
    // Precondition: This GenericLog is not full. 
    // 
    // Places element into this GenericLog. 

    boolean isFull(); 
    // Returns true if this GenericLog is full, otherwise returns false. 

    int size(); 
    // Returns the number of Strings in this GenericLog. 

    boolean contains(T element); 
    // Returns true if element is in this GenericLog, 
    // otherwise returns false. 
    // Ignores case differences when doing string comparison. 

    void clear(); 
    // Makes this GenericLog empty. 

    String getName(); 
    // Returns the name of this GenericLog. 

    String toString(); 
    // Returns a nicely formatted string representing this GenericLog. 
} 

LLGenericNode.java

package ch02.genericStringLogs; 

public class LLGenericNode<T> 
{ 
    private T info; 
    private LLGenericNode link; 

    public LLGenericNode(T info) 
    { 
    this.info = info; 
    link = null; 
    } 

    public void setInfo(T info) 
    // Sets info of this LLGenericNode. 
    { 
    this.info = info; 
    } 

    public T getInfo() 
    // Returns info of this LLGenericNode. 
    { 
    return info; 
    } 

    public void setLink(LLGenericNode link) 
    // Sets link of this LLGenericNode. 
    { 
    this.link = link; 
    } 

    public LLGenericNode getLink() 
    // Returns link of this LLGenericNode. 
    { 
    return link; 
    } 
} 

LinkedGenericLog.java

package ch02.genericStringLogs; 

public class LinkedGenericLog<T> implements GenericLogInterface 
{ 
    protected LLGenericNode log; // reference to first node of linked 
           // list that holds the GenericLog items 
    protected String name;  // name of this GenericLog 

    public LinkedGenericLog(String name) 
    // Instantiates and returns a reference to an empty GenericLog object 
    // with name "name". 
    { 
    log = null; 
    this.name = name; 
    } 

    public void insert(T element) 
    // Precondition: This GenericLog is not full. 
    // 
    // Places element into this GenericLog. 
    { 
    LLGenericNode newNode = new LLGenericNode(element); 
    newNode.setLink(log); 
    log = newNode; 
    } 

    public boolean isFull() 
    // Returns true if this GenericLog is full, false otherwise. 
    { 
    return false; 
    } 

    public int size() 
    // Returns the number of items in this GenericLog. 
    { 
    int count = 0; 
    LLGenericNode node; 
    node = log; 
while (node != null) 
    { 
     count++; 
     node = node.getLink(); 
    } 
    return count; 
    } 

    public boolean contains(T element) 
    // Returns true if element is in this GenericLog, 
    // otherwise returns false. 
    // Ignores case difference when doing comparison. 
    { 
    LLGenericNode node; 
    node = log; 

    while (node != null) 
    { 
     if (element.equals(node.getInfo())) // if they match 
     return true; 
     else 
     node = node.getLink(); 
    } 

    return false; 
    } 

    public void clear() 
    // Makes this GenericLog empty. 
    { 
    log = null; 
    } 

    public String getName() 
    // Returns the name of this GenericLog. 
    { 
    return name; 
    } 

    public String toString() 
    // Returns a nicely formatted string representing this GenericLog. 
    { 
    String logString = "Log: " + name + "\n\n"; 
    LLGenericNode node; 
node = log; 
    int count = 0; 

    while (node != null) 
    { 
     count++; 
     logString = logString + count + ". " + node.getInfo() + "\n"; 
     node = node.getLink(); 
    } 

    return logString; 
    } 
} 

您可以清楚地,明白了,我已經覆蓋包括()在LinkedGenericLog.java 然而,編譯器仍然拋出我這個錯誤。 我想這跟我在contains()方法的參數使用泛型的事,但我相信新的仿製藥和無法理解的問題。

任何人都可以幫助我嗎?

(順便說一句我正在運行的Java版本「1.6.0_15」,並以命令行編譯)

+1

什麼是LinkedGenericLog中的T? – 2013-02-27 21:20:47

+0

T表示LinkedGenericLog要存儲的項目的類型。換句話說,您可以實例化一個LinkedGenericLog,其中T被String替換,然後使用Log存儲String對象。 – bean 2013-02-27 21:22:01

+0

看到我的答案,然後 – 2013-02-27 21:23:15

回答

3

在Java方法簽名包括所述參數和它們的類型。所以,我不得不說的TLinkedGenericLog類型是從一個不同的GenericLogInterface

public class LinkedGenericLog<T> implements GenericLogInterface 

此行並不表明T是一樣的,你應該將其替換爲:

public class LinkedGenericLog<T> implements GenericLogInterface<T> 
+0

非常感謝,錯誤沒有顯示。我會設置這個問題來解決:) – bean 2013-02-27 21:25:27

+0

@ user2117153:請注意,這不是所有*你應該做的。你正在使用原始類型 - 查看我的答案。 – 2013-02-27 21:26:14

2

的問題是,你實現原料類型的GenericLogInterface。這導致了各種各樣的問題,涉及兩種不同的類型參數。所有你需要的就是改變這種:

public class LinkedGenericLog<T> implements GenericLogInterface<T> 

這樣:

public class LinkedGenericLog<T> implements GenericLogInterface 

那麼你應該停止使用原始類型LLGenericNode。所以:

protected LLGenericNode log; 

應該是:

protected LLGenericNode<T> log; 

,這(在insert):

LLGenericNode newNode = new LLGenericNode(element); 

應該是:在的LLGenericNode

LLGenericNode<T> newNode = new LLGenericNode<T>(element); 

同上使用和。

...並且也在LLGenericNode本身內。基本上,無論你在哪裏使用原始類型,請停止這樣做:)

+0

謝謝喬恩。我肯定會研究這些問題,並對原始類型進行一些研究,以便我將來不會犯這些錯誤。 – bean 2013-02-27 21:27:27