java 实现相同接口的类之间的关系
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13458026/
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
Relationships between classes that implement same interface
提问by rootpanthera
I'm pretty much java beginner, and i've come to ask stackoverflow forum for one simple question. I already checked similar question here on SO, but i didn't found the desired answer, LINK: Is there any relation between the class that implements interface and that interface?
我几乎是 Java 初学者,我来向 stackoverflow 论坛询问一个简单的问题。我已经在 SO 上检查过类似的问题,但我没有找到想要的答案,链接:实现接口的类和该接口之间有任何关系吗?
We know if Catextends Animal, there is a relationship here. So Cat is-a Animal ( but not necessarily otherwise. Animal is also a Dog..). We know this relationship as "is-a" - inheritance.
我们知道如果Cat扩展Animal,这里有一个关系。所以猫是动物(但不一定是动物。动物也是狗……)。我们将这种关系称为“is-a” -继承。
If we have another two classes: Girland Candyand if class Girl holds instance of Candy, then we name this relationship "has-a"-composition, because we can say that Girls has-a Candy.
如果我们还有另外两个类:Girl和Candy,并且如果类 Girl 持有 Candy 的实例,那么我们将这种关系命名为“has-a”- composition,因为我们可以说 Girls has-a Candy。
Correct me if i made something wrong with above examples.
如果我在上面的例子中做错了什么,请纠正我。
Anyway my question is if its there some relationship between objects of classes that implement the same interface? Let's say:
无论如何,我的问题是实现相同接口的类的对象之间是否存在某种关系?让我们说:
public class A implements InterfaceA {}
public class B implements InterfaceA {}
If there is some relationship, how we can make use of that relationship? Thanks for your answers in advance..
如果存在某种关系,我们如何利用这种关系?感谢您提前回答..
EDIT: For those who think that there isn't a relationship, i have a book "java how to program ninth edition" and there is a UML diagram which shows classes that implements same interface in "relationship" ?
编辑:对于那些认为没有关系的人,我有一本书“java 如何编程第九版”,并且有一个 UML 图显示了在“关系”中实现相同接口的类?
Image:
图片:
采纳答案by kosa
There is no relationship between the objects which implement same interface. Both are independent implementations of their own. They just follow the protocol defined by interface.
实现相同接口的对象之间没有关系。两者都是它们自己的独立实现。它们只是遵循接口定义的协议。
If you try to cast from one object type to another object type, you will end-up getting ClassCastException
.
如果您尝试从一种对象类型转换为另一种对象类型,您最终会得到ClassCastException
.
EDIT:
编辑:
Your UML diagram suggest that there is relationship between interface and class (IS-A), but not between the two classes which implement same interface. There is no line with relationship symbol between the classes (like you see between class and interface)
您的 UML 图表明接口和类(IS-A)之间存在关系,但实现相同接口的两个类之间没有关系。类之间没有关系符号(就像你在类和接口之间看到的那样)
回答by svz
This means that your classes have similar behaviour described by the interface methods.
这意味着您的类具有由接口方法描述的类似行为。
For example, consider two classes: Fish
and Bird
. They both can implement interface Moveable
like:
例如,考虑两个类:Fish
和Bird
。它们都可以实现如下接口Moveable
:
inteface Moveable{
void move(int x, int y, int z);
}
class Fish implements Moveable{
public void move(int x, int y, int z){
swim(x, y, z);
}
private void swim(int x, int y, int z){
//a method describing how the fish swims
}
}
class Bird implements Moveable{
public void move(int x, int y, int z){
fly(x, y, z);
}
private void fly(int x, int y, int z){
//a method describing how the bird flies
}
}
You can call the behaviour specified by interface methods like:
您可以调用由接口方法指定的行为,例如:
Moveable bird = new Bird();
Moveable fish = new Fish();
bird.move(10,10,10);
fish.move(10,10,-10);
So either of them can move, though the way they move differs greatly. This way both classes share common behaviour though its implementation may be unknown to you.
所以他们中的任何一个都可以移动,尽管他们移动的方式有很大的不同。这样,两个类共享共同的行为,尽管您可能不知道它的实现。
回答by Tom Anderson
There is no formal name for the relationship between two classes which implement the same interface.
实现相同接口的两个类之间的关系没有正式名称。
Or, for that matter, which extend the same base class. If a child which extends another is its child, then two children of the same class are siblings, but this is not a term which is generally used.
或者,就此而言,它们扩展了相同的基类。如果继承另一个的孩子是它的孩子,那么同一类的两个孩子是兄弟姐妹,但这不是一个普遍使用的术语。
If you wanted to extend this anthropomorphic metaphor to interfaces, you could call two implementations of an interface look-alikes, perhaps.
如果你想把这个拟人化的比喻扩展到接口,你可以调用一个接口相似的两个实现,也许。
回答by Michael Berry
The relationship between a class implementing and interface, and the interface is also an "is-a" relationship, the same as extending from a class.
类实现与接口的关系,接口也是“is-a”的关系,与从类继承一样。
It's useful (or at least one reason it's useful) because by providing an interface, you can code to an interface rather than code to an implementation of that interface, making it easy (if you ever need to) to swap your implementations over with the minimum of fuss.
它很有用(或至少有一个有用的原因),因为通过提供接口,您可以对接口进行编码,而不是对接口的实现进行编码,从而可以轻松(如果需要的话)将您的实现与最少的麻烦。
Note that if you're going "across" the inheritance hierarchy, so looking at the relationship between two classes which inherit the same interface, there is no implicit relationship here at all.
请注意,如果您要“跨越”继承层次结构,那么查看继承相同接口的两个类之间的关系,这里根本没有隐式关系。