Java编译时错误:编译器无法识别方法重写
我正在修改Java中日志的一个相当基本的链表实现,以使用泛型。我的项目有3.java文件:Java编译时错误:编译器无法识别方法重写,java,Java,我正在修改Java中日志的一个相当基本的链表实现,以使用泛型。我的项目有3.java文件: GenericLogiInterface.java->定义日志的接口 LLGenericNode.java->定义LL节点的类 linkedenericlog.java->定义LL的接口扩展 不幸的是,我从LinkedGenericLog.java收到一个编译时错误: ch02/genericstringglogs/linkedenericlog.java:10:ch02.genericstringg
- GenericLogiInterface.java->定义日志的接口
- LLGenericNode.java->定义LL节点的类
- linkedenericlog.java->定义LL的接口扩展
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.
}
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;
}
}
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;
}
}
包ch02.genericstringglogs;
公共接口通用逻辑接口
{
空心插件(T元件);
//前提条件:此常规日志未满。
//
//将元素放置到此GenericLog中。
布尔值为full();
//如果此GenericLog已满,则返回true,否则返回false。
int size();
//返回此GenericLog中的字符串数。
布尔包含(T元素);
//如果元素在此GenericLog中,则返回true,
//否则返回false。
//在进行字符串比较时忽略大小写差异。
无效清除();
//使此GenericLog为空。
字符串getName();
//返回此GenericLog的名称。
字符串toString();
//返回表示此GenericLog的格式良好的字符串。
}
LLGenericNode.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.
}
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;
}
}
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;
}
}
包ch02.genericstringglogs;
公共类LLGenericNode
{
私人信息;
专用LLGenericNode链路;
公共LLGenericNode(T信息)
{
this.info=info;
link=null;
}
公共无效设置信息(T信息)
//设置此LLGenericNode的信息。
{
this.info=info;
}
公共T getInfo()
//返回此LLGenericNode的信息。
{
退货信息;
}
public void setLink(LLGenericNode链接)
//设置此LLGenericNode的链接。
{
this.link=link;
}
public LLGenericNode getLink()
//返回此LLGenericNode的链接。
{
返回链接;
}
}
linkedenericlog.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.
}
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;
}
}
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;
}
}
包ch02.genericstringglogs;
公共类LinkedGenericLog实现GenericLogiInterface
{
受保护的LLGenericNode日志;//对链接的第一个节点的引用
//保存GenericLog项的列表
受保护的字符串名称;//此GenericLog的名称
公共LinkedGenericLog(字符串名称)
//实例化并返回对空GenericLog对象的引用
//名称为“name”。
{
log=null;
this.name=名称;
}
公共空白插入(T元素)
//前提条件:此常规日志未满。
//
//将元素放置到此GenericLog中。
{
LLGenericNode newNode=新的LLGenericNode(元素);
setLink(日志);
log=newNode;
}
公共布尔值isFull()
//如果此GenericLog已满,则返回true,否则返回false。
{
返回false;
}
公共整数大小()
//返回此GenericLog中的项目数。
{
整数计数=0;
LLGenericNode;
节点=日志;
while(节点!=null)
{
计数++;
node=node.getLink();
}
返回计数;
}
公共布尔包含(T元素)
//如果元素在此GenericLog中,则返回true,
//否则返回false。
//进行比较时忽略大小写差异。
{
LLGenericNode;
节点=日志;
while(节点!=null)
{
if(element.equals(node.getInfo())//如果匹配
返回true;
其他的
node=node.getLink();
}
返回false;
}
公共空间清除()
//使此GenericLog为空。
{
log=null;
}
公共字符串getName()
//返回此GenericLog的名称。
{
返回名称;
}
公共字符串toString()
//返回表示此GenericLog的格式良好的字符串。
{
String logString=“Log:+name+”\n\n”;
LLGenericNode;
节点=日志;
整数计数=0;
while(节点!=null)
{
计数++;
logString=logString+count+“+node.getInfo()+”\n”;
node=node.getLink();
}
返回日志字符串;
}
}
您可以清楚地看到,我已经覆盖了linkedenericlog.java中的contains()
然而,编译器仍然向我抛出这个错误。
我认为这与我在contains()方法的参数中使用泛型有关,但我对泛型不熟悉,不能理解这个问题
有人能帮我吗
(顺便说一句,我正在运行java版本“1.6.0_15”,并使用命令行进行编译)在java中,方法签名包括参数及其类型。因此我不得不说
linkedenericlog
中的T
类型与GenericLogInterface
public class LinkedGenericLog<T> implements GenericLogInterface
公共类LinkedGenericLog实现GenericLogiInterface
这一行并不表示t是相同的,您应该将其替换为:
public class LinkedGenericLog<T> implements GenericLogInterface<T>
公共类LinkedGenericLog实现GenericLogiInterface
在java中,方法签名包括参数及其类型。因此我不得不说linkedenericlog
中的T
类型与GenericLogInterface
public class LinkedGenericLog<T> implements GenericLogInterface
公共类LinkedGenericLog实现GenericLogiInterface
这一行并不表示t是相同的,您应该将其替换为:
public class LinkedGenericLog<T> implements GenericLogInterface<T>
公共类LinkedGenericLog实现GenericLogiInterface
问题在于您正在实现GenericLogiInterface
的原始类型。这导致了各种各样的问题,有两个不同类型的参数