简体   繁体   English

对Java接口的怀疑实现

[英]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 . 并创建一个名为BeautifulPinkPhoneSimplePhone子类。 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? 您是否尝试过在主类中没有接口实现进行编译?

Phone3Electronic类继承它。

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.

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