Java 实现类的接口:类到接口到接口
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19384298/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
implementing a interface to a class: class to interface to interface
提问by comodeque
hi im a beginner in this and im trying to implement a class to a interface. The interface extends another interface. Im making a class with methods to run through a list and interfer with it, the two interfaces are apart of this.
嗨,我是这方面的初学者,我正在尝试为接口实现一个类。该接口扩展了另一个接口。我创建了一个带有方法的类来运行一个列表并干扰它,这两个接口是分开的。
The two interfaces are in each its separate file, but everything is in the same package, hope this is right.
这两个接口都在各自独立的文件中,但一切都在同一个包中,希望这是正确的。
Im getting the following errors:
我收到以下错误:
From the class doublelinkedlist: interface expected here
来自类双链表: interface expected here
From the interface A: doublelinkedlist.A is already defined in doublelinkedlist, interface expected here
从接口A: doublelinkedlist.A is already defined in doublelinkedlist, interface expected here
From interface B: doublelinkedlist.B is already defined in doublelinkedlist
从接口 B: doublelinkedlist.B is already defined in doublelinkedlist
Code class:
代码类:
package doublelinkedlist;
import java.util.*;
public class Doublelinkedlist<T> implements A<T>
{
Code interface A: (in separate file called A.java )
代码接口 A:(在名为 A.java 的单独文件中)
package doublelinkedlist;
import java.util.Iterator;
public class A<T> { // am I supposed to have a class here? or just have the interface?
public interface A<T> extends B<T>
{
Code interface B: (in separate file called B.java )
代码接口 B:(在名为 B.java 的单独文件中)
package doublelinkedlist;
public class B<T> {
public interface B<T> extends Iterable<T>
{
There is no code for the two interfaces in the class, so I dont understand why i get an error saying its already defined. Anybody got a clue?
类中的两个接口没有代码,所以我不明白为什么我会收到一个错误,说它已经定义了。有人有线索吗?
采纳答案by Klazen108
interface expected here
indicates that the compiler is expecting an interface after implements
, where you have given it a class (since the declaration for class A<T>
came first).
interface expected here
表示编译器在 之后需要一个接口implements
,您已经给它一个类(因为class A<T>
首先声明了)。
doublelinkedlist.A is already defined in doublelinkedlist, interface expected here
is thrown because you have a class named A and an interface inside the class named A, they can't both have the same name (same with B)
doublelinkedlist.A is already defined in doublelinkedlist, interface expected here
抛出是因为您有一个名为 A 的类和一个名为 A 的类中的接口,它们不能具有相同的名称(与 B 相同)
While you can technically have an interface inside of a class, that's not really the best design (since interfaces were intended to abstract, or hide, away the details of the underlying classes, they are typically more visible), so try to avoid doing that unless you have a good reason :)
虽然从技术上讲,您可以在类中拥有一个接口,但这并不是真正的最佳设计(因为接口旨在抽象或隐藏底层类的细节,因此它们通常更明显),因此请尽量避免这样做除非你有充分的理由:)
This is probably what you're looking for:
这可能是你正在寻找的:
Doublelinkedlist.java:
双链表.java:
package doublelinkedlist;
import java.util.*;
public class Doublelinkedlist<T> implements A<T>
{...}
A.java:
A.java:
package doublelinkedlist;
import java.util.Iterator;
public interface A<T> extends B<T>
{...}
B.java:
B.java:
package doublelinkedlist;
public interface B<T> extends Iterable<T>
{...}
Further reading on interfaces: http://docs.oracle.com/javase/tutorial/java/IandI/createinterface.html
关于接口的进一步阅读:http: //docs.oracle.com/javase/tutorial/java/IandI/createinterface.html
回答by Fildor
public class A<T> { // am I supposed to have a class here? or just have the interface?
public interface A<T> extends B<T>
{
This is ambigous. Is A the interface or the class?
这是模棱两可的。A 是接口还是类?
Only interfaces can be "implemented". You cannot implement a class.
只能“实现”接口。你不能实现一个类。
So I guess your issue is done by changing those into:
所以我想你的问题是通过将它们更改为:
File DoubleLinkedList.java:
文件 DoubleLinkedList.java:
public class Doublelinkedlist<T> implements A<T>
{
// ... blah blah
}
-> The classimplements interface A
->该类实现了接口 A
File A.java:
文件 A.java:
public interface A<T> extends B<T>
{
// ...
}
-> The interface extends interface B
-> 接口扩展接口B
File B.java:
文件 B.java:
public interface B<T> extends Iterable<T>
{
// ...
}
-> The interface extends interface Iterable
-> 接口扩展接口 Iterable
回答by tommy knocker
public interface Entity{
public int getId();
public void setId(int id);
public int getHealth();
public void setHealth(int health);
}
public class Tank implements Entity{
//without implementing the methods mentioned in Entity you will immediately see a red line //under "Tank" in your IDE saying un-implemented methods.
}
public class Tank implements Entity{
private int id;
private int health;
public Tank(){..}
public int getId(){
return id;
}
public void setId(int id){
this.id = id;
}
public int getHealth(){
return health;
}
public void setHealth(int health){
this.health = health;
}
}
an interface is a contract between the class that is implementing the interface, it is saying "HEY YOU, you have signed up to use me, so you better take these methods and define them properly".
接口是实现接口的类之间的契约,它说“嘿,你已经注册使用我,所以你最好采用这些方法并正确定义它们”。
An interface will only describe method headers, but not actually offer any content to it, it is left to the class that implements the interface.
接口只会描述方法头,但实际上并不为其提供任何内容,它留给实现接口的类。
further more, understand the following inheritence - enables a class to inherit the attributes and methods of its parent class, but the same child class cannot inherit directly from 2 parent classes, you can however have a child class inherit from a parent class which inherits from another class which implements an interface. here is an example
此外,了解以下继承 - 使类能够继承其父类的属性和方法,但同一个子类不能直接从 2 个父类继承,但是您可以让子类从继承自的父类继承另一个实现接口的类。这是一个例子
public interface Entity{
...
}
public Soldier implements Entity{
// implements the entity interface,
//and defines the methods
}
public SwordsMan extends Soldiers{
//this class inherits the methods and attributes of soldier
//which had also implemented the methods from entity
}
You also mention trying to extend an interface into an interface, interfaces are designed with no implemented methods, if you want to use two interfaces you are best to do this:
您还提到尝试将接口扩展为接口,接口的设计没有实现方法,如果您想使用两个接口,最好这样做:
public class Aircraft implements Engine, Entity{
//this class then has to implements all methods from Engine and Entity
}