[英]Doubt in Java interface implements
interface Device
{
public void doIt();
}
public class Electronic implements Device
{
public void doIt()
{
}
}
abstract class Phone1 extends Electronic
{
}
abstract class Phone2 extends Electronic
{
public void doIt(int x)
{
}
}
class Phone3 extends Electronic implements Device
{
public void doStuff()
{
}
}
Can any one tell me why this compiles.. Because "Phone3" implements Device and it should have doIt() method but it does not have. 任何人都可以告诉我为什么这个编译..因为“Phone3”实现了Device,它应该有doIt()方法,但它没有。 But still this compiles. 但这仍然是编译。 May i know Y? 我可以认识Y吗?
Phone3
extends Electronic
, and Electronic
has the method doIt()
, implementing the Device
interface. Phone3
扩展Electronic
,而Electronic
有方法doIt()
,实现Device
接口。 The implementation of the doIt
method is thus just inherited from the Electronic
base class. 因此, doIt
方法的实现仅从Electronic
基类继承。
It makes sense if you make the example more realistic. 如果你让这个例子更加真实,那就更有意义了。 Change Device
to Ringable
, with a ring
method. 使用ring
方法将Device
更改为可Ringable
Device
。 Create a base class SimplePhone
implementing Ringable
, with an implementation of the ring
method. 创建一个实现Ringable
的基类SimplePhone
,并实现ring
方法。 And make a subclass of SimplePhone
called BeautifulPinkPhone
. 并创建一个名为BeautifulPinkPhone
的SimplePhone
子类。 The beautiful pink phone will be able to ring because it's just a simple phone with a pink color. 美丽的粉红色手机可以响,因为它只是一个粉红色的简单手机。
implements Device
is redundant in class Phone3
definition. 在Phone3
类定义中, implements Device
是冗余的。 The class in inheriting the fact of implementing the Device
interface from the Electronic
class. 该类继承了从Electronic
类实现Device
接口的事实。
That is, every class extending Electronic
is implementing Device
also, and is also inheriting the implementation of doIt
that Electronic
provides. 也就是说,每个扩展Electronic
类都在实现Device
,并且还继承了Electronic
提供的doIt
的实现。 Every one of them can extend/provide a different implementation of doIt
by overriding it. 他们中的每一个都可以通过覆盖来扩展/提供不同的doIt
实现。
这是因为Phone3扩展了Electronic和Electronic已经实现了doIt()方法。
it works, because Electronic
implements Device
. 它工作,因为Electronic
实现Device
。 have you tried compiling without interface implementation in main class? 您是否尝试过在主类中没有接口实现进行编译?
Phone3
从Electronic
类继承它。
Phone3 extends Electronic and inherits all it's methods. Phone3扩展了Electronic并继承了它的所有方法。 Since Electronic has a doIt() method it compiles. 由于Electronic有一个doIt()方法,它编译。
Phone3
扩展了Electronic
因此它继承了doIt()
方法
If you see in a above code, Phone3 already implemented doIt() method in a inheritance tree,ie in a Electronic class, and if you wish to call doIt() method in your Phone3 class it'll work fine such as. 如果您在上面的代码中看到,Phone3已经在继承树中实现了doIt()方法,即在Electronic类中,并且如果您希望在Phone3类中调用doIt()方法,它将正常工作,例如。
class Phone3 extends Electronic implements Device
{
public void doStuff()
{
}
public static void main(String...args)
{
Phone3 p3=new Phone3();
p3.doIt();
}
}
Phone3
扩展了Electronic
,它已经实现了doIt
。
Cause Phone3 also extends Electronic which has a empty implementation for the doIt method. 原因Phone3还扩展了Electronic,它具有doIt方法的空实现。
So it does not need to have it, unless it needs to override the behaviour. 所以它不需要它,除非它需要覆盖行为。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.