Java でのログのかなり基本的なリンク リスト実装をジェネリックを使用するように変更する作業を行っています。私のプロジェクトには 3 つの .java ファイルがあります。
- GenericLogInterface.java -> ログを定義するインターフェース
- LLGenericNode.java -> LL のノードを定義するクラス
- LinkedGenericLog.java -> LL を定義するインターフェースの拡張
残念ながら、LinkedGenericLog.java からコンパイル時エラーが発生します。
ch02/genericStringLogs/LinkedGenericLog.java:10: ch02.genericStringLogs.LinkedGenericLog は抽象化されておらず、ch02.genericStringLogs.GenericLogInterface の抽象メソッド contains(java.lang.Object) をオーバーライドしません
これは、LinkedGenericLog.java 内の GenericLogInterface.java の contains() メソッドをオーバーライドすることで簡単に解決できるようです。
ただし、大きなハングアップが 1 つあります。それは、既にオーバーライドしてしまっていることです。
3 つの .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() をオーバーライドしましたが、コンパイラはまだこのエラーをスローします。contains() メソッドの引数でのジェネリックの使用に関係していると考えていますが、ジェネリックは初めてで、問題を理解できません。
誰でも私を助けることができますか?
(ちなみに、Javaバージョン「1.6.0_15」を実行しており、コマンドラインでコンパイルしています)