简体   繁体   English

实现一个Java接口

[英]Implementing a java interface

I have an interface that I want to implement that looks like this, but with more methods. 我有一个要实现的接口,看起来像这样,但是有更多的方法。 There is just one here for example: 例如,这里只有一个:

public interface List{

public void add(int position, Album album);

}

Then my class that implements it starts like this, including all the methods present within the interface: 然后,实现它的我的类就这样开始,包括接口中存在的所有方法:

public class SongList implements List{

public void add(int position, Album album){
...my code for this method
}

My compiler tells me two errors. 我的编译器告诉我两个错误。 The first - when compiling my List interface - says that the interface List cannot find my Song class: 第一个-编译List接口时-说接口List找不到我的Song类:

List.java:23: error: cannot find symbol
public void add(int position, Song item);
                            ^
symbol:   class Song
location: interface List
1 error

I have a song class that compiles and is in the same folder as the interface and all of my other java files. 我有一个可编译的歌曲类,与接口和所有其他Java文件位于同一文件夹中。
Second - when compiling SongList - the compiler says that I haven't overridden the add() method: 其次-编译SongList时-编译器说我还没有覆盖add()方法:

SongList is not abstract and does not override abstract method add(int,Object) in List
public class SongList implements List{
       ^
1 error

I'm pretty lost here...as far as I have googled I am following all of the rules for interfaces, but apparently not. 我在这里很迷路...就我搜索过的而言,我遵循所有接口规则,但显然没有。 Any ideas what I'm doing wrong? 有什么想法我做错了吗?

The first and second code blocks indicate that the add method is expecting an Album, but the third and fourth code blocks indicate that you've implemented an add method with a Song parameter. 第一个和第二个代码块指示add方法需要专辑,但是第三个和第四个代码块指示您已实现了带有Song参数的add方法。 Either the interface needs an add method that expects a Song, or else your class needs to implement an add method that expects an Album - the methods need the same parameters. 接口要么需要一个需要Song的add方法,要么您的类需要实现一个需要一个Album的add方法-这些方法需要相同的参数。

I think that you are importing the wrong List interface. 我认为您正在导入错误的List接口。

Please check that your List interface is mentioned in the import statements and no other List interface are imported as the error statement says.. 请检查导入语句中是否提到了您的List接口,并且未按照错误语句中的说明导入其他List接口。

add(int,Object) 添加(int,Object)

and not 并不是

add(int,Album) 加(int,专辑)

I think that you are accidentally implementing the java list interface . 我认为您不小心实现了Java列表接口

I hope this is helpful.. 我希望这是有帮助的..

For your first problem, clearly the compiler is having trouble locating Song.class. 对于第一个问题,显然编译器在定位Song.class时遇到麻烦。 If Song.java is in the same directory, ensure that it has been compiled. 如果Song.java在同一目录中,请确保已对其进行编译。

For the second problem, implementing an interface means you have to implement each of it's methods. 对于第二个问题,实现接口意味着您必须实现其每个方法。 That means you need to write a method with the exact same method signature - which includes the parameters. 这意味着您需要编写具有完全相同的方法签名(包括参数)的方法。

Though this time, since you didn't include all the methods in your interface, I'll just assume they're all implemented. 尽管这次,由于您没有在界面中包括所有方法,因此我假设它们都已实现。 In which case it looks like R.daneel.olivaw is correct, you might be using the wrong List. 在这种情况下,看起来R.daneel.olivaw是正确的,则可能使用了错误的列表。 Ensure the signatures match, and that your version of List is being used. 确保签名匹配,并且正在使用您的List版本。 As mentioned above, that means it has to be compiled and located on java's classpath (or in the same directory). 如上所述,这意味着必须对其进行编译,并将其放在java的classpath上(或同一目录中)。

Edit: Originally misinterpreted the second part of the question, this answer has been updated accordingly. 编辑:最初误解了问题的第二部分,此答案已相应更新。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM