我正在努力修改Java中日志的相当基本的链表实现以使用泛型。我的项目有3个.java文件:
不幸的是,我从LinkedGenericLog.java收到编译时错误:
ch02 / genericStringLogs / LinkedGenericLog.java:10:ch02.genericStringLogs.LinkedGenericLog不是抽象的,并且不覆盖ch02.genericStringLogs.GenericLogInterface中的抽象方法contains(java.lang.Object)
这似乎很容易通过覆盖LinkedGenericLog.java中GenericLogInterface.java的contains()方法来解决。
然而,有一个巨大的挂断: 我已经覆盖了它。
以下是三个.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”并使用命令行进行编译)
答案 0 :(得分:3)
在java中,方法签名包括参数及其类型。所以我不得不说T
中LinkedGenericLog
的类型与GenericLogInterface
public class LinkedGenericLog<T> implements GenericLogInterface
此行不表示T相同,您应将其替换为:
public class LinkedGenericLog<T> implements GenericLogInterface<T>
答案 1 :(得分:2)
问题是您正在实施{em> raw 类型的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<T> newNode = new LLGenericNode<T>(element);
在LLGenericNode
和toString()
中同意使用contains()
。
......也在LLGenericNode
内。基本上,无论你在哪里使用原始类型,都要停止这样做:)