简体   繁体   English

将接口扩展到另一个接口

[英]Extending an interface to another interface

Let's say I have 2 interfaces here, Moveable and Walkable (sorry for the bad example, but please if you have a better one kindly post it) 比方说,我有2个接口,在这里, MoveableWalkable (遗憾的坏榜样,但请,如果你有一个更好的亲切张贴)

interface Runnable{
    void run();
}

interface Walkable extends Runnable {
    void walk();
}

public class Human implements Walkable {

}

And the interface Walkable is a subclass of Runnable , when the Human class implements the Walkable interface should the Human class provide implementations for void walk() from the interface Walkable and void run() from the interface Runnable ? 而接口Walkable是的子类Runnable ,当Human类实现了Walkable接口应该在Human类为实现void walk()从接口Walkablevoid run()从接口Runnable does the interface Walkable inherit the abstract method run() from the interface Runnable ? Walkable接口是否从Runnable接口继承了抽象方法run()

是的,您的Human类需要同时实现walk()和run()方法。

should the human class provide implementations for void walk from the interface walkable and void run() from the interface runnable?

Yes , when your Walkable interface extends Runnable it also inherits run method means now if Human class is implementing Walkable interface it has to implement both the methods otherwise it should be abstract . 是的,当你的Walkable接口扩展Runnable ,它还继承了run方法,如果Human类正在实现Walkable接口,它必须实现这两个方法,否则它应该是abstract

Implementing an Interface is a contract where Implementing class has to implement all the methods declared in Interface. 实现Interface是一个契约,其中实现类必须实现Interface中声明的所有方法。

does the interface Walkable inherit the abstract method run() from the interface Runnable?

Yes, It is the OOPS Inheritance concept. 是的,这是OOPS Inheritance概念。

when the Human class implements the Walkable interface should the human class provide implementations for void walk from the interface walkable and void run() from the interface runnable? 当Human类实现Walkable接口时,人类应该从可运行接口的walkable和void run()接口提供void walk的实现吗?

Yes. 是。 You could have discovered this very easily by trying to compile the code. 您可以通过尝试编译代码来轻松地发现这一点。 The compiler would complain because there are no implementstions of the run and walk methods in class Human . 编译器会抱怨因为类Human中没有runwalk方法的实现。

does the interface walkable inherits the abstract method run() from the interface runnable? 接口walkable是否从runnable接口继承了抽象方法run()?

Yes. 是。

Walkable是Runnable,因此必须定义run()函数。

Yes your human should walk AND run . 是的,你的人应该walkrun

Which would mean to invert the thing no? 哪个意味着颠倒事物没有? If you run you can certainly walk.But someone injured could walk without running ... 如果你跑步,你肯定可以走路。但是受伤的人可以不跑步走路......

yes, an interface can extend multiple interfaces and the implementing class should implement all the methods inherited by the implementing interface. 是的,接口可以扩展多个接口,实现类应该实现实现接口继承的所有方法。

So yes to your question, you must implement both methods 对你的问题是肯定的,你必须实现这两种方法

Human class needs to implement both methods, otherwise - you must declare it abstract. 人类需要实现这两种方法,否则 - 你必须将它声明为抽象。 And yes, Walkable interface inherits the method run() from Runnable interface. 是的,Walkable接口从Runnable接口继承run()方法。

You can learn more about interfaces and inheritance here . 您可以在此处了解有关接口和继承的更多信息。

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

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