我真的很难理解哪里出了问题。从这个开始,我有一个像这样的界面...
public interface UserRecordsInterface
{
public abstract List getRecordsVector()
throws UserExitException;
} 我想在我的类MyRecordsClass.java中实现这个接口,如下所示...
public class MyRecordsClass implements UserRecordsInterface{
private List addendaRecs = null;
/**
* Return the list of addenda records
*
* @return List
*/
public List getRecordsVector() {
return addendaRecs;
}
} 在使用ant进行编译时,我得到了两个错误。
1个Class MyRecordsClass is not abstract and does not override the abstract method getRecordsVector() in UserRecordInterface.
2个getRecordsVector() in MyRecordsClass cannot implement getRecordsVector() in UserRecordsInterface; attempting to use incompatible return type.
- [javac] found : java.util.List
- [javac] required: java.util.Vector
- [javac] public List getRecordsVector() {最初,方法getRecordsVector()在接口中有返回类型Vector.现在,它被更改为List。所以,我在课堂上做了相应的改变。现在,它给出了这个错误。如果我把我的类改为Vector & compile,那么它就能正常工作。但是我想使用List,因为这是当前接口所具有的。所以,我相信ant仍然指向具有向量接口的旧lib。不确定这是ant的问题还是我的代码有问题。请建议..
发布于 2014-03-13 21:21:25
您不需要在接口中使用abstract修饰符,我有一种预感,正是它导致了您的问题。使用抽象方法的唯一地方是在抽象类中。
编辑
我认为很可能是接口最近发生了变化,并且位于一个单独的jar中。
它可能曾经返回一个Vector,但现在您看到的源代码返回一个List。
我怀疑您正在使用类路径中的旧版本jar (其中getRecordsVector()仍然返回Vector)进行编译。
发布于 2014-03-13 23:49:40
您使用的是什么版本的Java?我在IntelliJ IDEA中运行Java7,它没有抱怨代码-它实际上已经编译了,如果我在里面放一个main方法,我就可以运行了。
我同意@NickJ的注释-接口方法本质上是抽象的-因为你必须在实现接口的类中实现它们。See this posting for more information.
https://stackoverflow.com/questions/22379630
复制相似问题