接口扩展另一个接口但实现其方法

时间:2020-12-12 17:33:07

In java when an interface extends another interface:

在java中,当一个接口扩展另一个接口时:

  1. Why does it implement its methods?
  2. 为什么要实现其方法?
  3. How can it implement its methods when an interface can't contain a method body
  4. 当接口不能包含方法体时,如何实现其方法
  5. How can it implement the methods when it extends the other interface and not implement it?
  6. 如何在扩展其他接口而不实现它时实现这些方法?
  7. What is the purpose of an interface implementing another interface?
  8. 实现另一个接口的接口的目的是什么?

This has major concepts in Java!

这有Java的主要概念!

EDIT:

编辑:

public interface FiresDragEvents {

  void addDragHandler(DragHandler handler);

  void removeDragHandler(DragHandler handler);
}


public interface DragController extends FiresDragEvents {

  void addDragHandler(DragHandler handler);

  void removeDragHandler(DragHandler handler);

  void dragEnd();

  void dragMove();
}

In eclipse there is the implement sign besides the implemented methods in DragController.

在eclipse中,除了DragController中实现的方法之外还有实现标志。

And when I mouse-hover it, it says that it implements the method!!!

当我鼠标悬停它,它说它实现了方法!

3 个解决方案

#1


49  

Why does it implement its methods? How can it implement its methods when an interface can't contain method body? How can it implement the methods when it extends the other interface and not implement it? What is the purpose of an interface implementing another interface?

为什么要实现其方法?当接口不能包含方法体时,如何实现其方法?如何在扩展其他接口而不实现它时实现这些方法?实现另一个接口的接口的目的是什么?

Interface does not implement the methods of another interface but just extends them. One example where the interface extension is needed is: consider that you have a vehicle interface with two methods moveForward and moveBack but also you need to incorporate the Aircraft which is a vehicle but with some addition methods like moveUp, moveDown so in the end you have:

接口不实现另一个接口的方法,只是扩展它们。需要接口扩展的一个例子是:考虑你有一个车辆接口有两个方法moveForward和moveBack,但你还需要合并飞机这是一个车辆,但有一些额外的方法,如moveUp,moveDown所以最后你有:

public interface IVehicle {
  bool moveForward(int x);
  bool moveBack(int x);
};

and airplane:

和飞机:

public interface IAirplane extends IVehicle {
  bool moveDown(int x);
  bool moveUp(int x);
};

#2


20  

An interface defines behavior. For example, a Vehicle interface might define the move() method.

接口定义行为。例如,Vehicle接口可能会定义move()方法。

A Car is a Vehicle, but has additional behavior. For example, the Car interface might define the startEngine() method. Since a Car is also a Vehicle, the Car interface extends the Vehicle interface, and thus defines two methods: move() (inherited) and startEngine().

汽车是一种车辆,但有其他行为。例如,Car接口可能会定义startEngine()方法。由于Car也是Vehicle,Car接口扩展了Vehicle接口,因此定义了两个方法:move()(inherited)和startEngine()。

The Car interface doesn't have any method implementation. If you create a class (Volkswagen) that implements Car, it will have to provide implementations for all the methods of its interface: move() and startEngine().

Car接口没有任何方法实现。如果您创建一个实现Car的类(Volkswagen),它将必须为其接口的所有方法提供实现:move()和startEngine()。

An interface may not implement any other interface. It can only extend it.

接口可能不实现任何其他接口。它只能扩展它。

#3


6  

ad 1. It does not implement its methods.

广告1.它没有实现其方法。

ad 4. The purpose of one interface extending, not implementing another, is to build a more specific interface. For example, SortedMap is an interface that extends Map. A client not interested in the sorting aspect can code against Map and handle all the instances of for example TreeMap, which implements SortedMap. At the same time, another client interested in the sorted aspect can use those same instances through the SortedMap interface.

ad 4.扩展一个接口而不是另一个接口的目的是构建一个更具体的接口。例如,SortedMap是一个扩展Map的接口。对排序方面不感兴趣的客户端可以对Map进行编码并处理例如实现SortedMap的TreeMap的所有实例。同时,对排序方面感兴趣的另一个客户端可以通过SortedMap接口使用这些相同的实例。

In your example you are repeating the methods from the superinterface. While legal, it's unnecessary and doesn't change anything in the end result. The compiled code will be exactly the same whether these methods are there or not. Whatever Eclipse's hover says is irrelevant to the basic truth that an interface does not implement anything.

在您的示例中,您将从超级接口重复这些方法。虽然合法,但它是不必要的,并且不会改变最终结果中的任何内容。无论这些方法是否存在,编译后的代码都完全相同。无论Eclipse的悬停说什么都与接口没有实现任何东西的基本事实无关。

#1


49  

Why does it implement its methods? How can it implement its methods when an interface can't contain method body? How can it implement the methods when it extends the other interface and not implement it? What is the purpose of an interface implementing another interface?

为什么要实现其方法?当接口不能包含方法体时,如何实现其方法?如何在扩展其他接口而不实现它时实现这些方法?实现另一个接口的接口的目的是什么?

Interface does not implement the methods of another interface but just extends them. One example where the interface extension is needed is: consider that you have a vehicle interface with two methods moveForward and moveBack but also you need to incorporate the Aircraft which is a vehicle but with some addition methods like moveUp, moveDown so in the end you have:

接口不实现另一个接口的方法,只是扩展它们。需要接口扩展的一个例子是:考虑你有一个车辆接口有两个方法moveForward和moveBack,但你还需要合并飞机这是一个车辆,但有一些额外的方法,如moveUp,moveDown所以最后你有:

public interface IVehicle {
  bool moveForward(int x);
  bool moveBack(int x);
};

and airplane:

和飞机:

public interface IAirplane extends IVehicle {
  bool moveDown(int x);
  bool moveUp(int x);
};

#2


20  

An interface defines behavior. For example, a Vehicle interface might define the move() method.

接口定义行为。例如,Vehicle接口可能会定义move()方法。

A Car is a Vehicle, but has additional behavior. For example, the Car interface might define the startEngine() method. Since a Car is also a Vehicle, the Car interface extends the Vehicle interface, and thus defines two methods: move() (inherited) and startEngine().

汽车是一种车辆,但有其他行为。例如,Car接口可能会定义startEngine()方法。由于Car也是Vehicle,Car接口扩展了Vehicle接口,因此定义了两个方法:move()(inherited)和startEngine()。

The Car interface doesn't have any method implementation. If you create a class (Volkswagen) that implements Car, it will have to provide implementations for all the methods of its interface: move() and startEngine().

Car接口没有任何方法实现。如果您创建一个实现Car的类(Volkswagen),它将必须为其接口的所有方法提供实现:move()和startEngine()。

An interface may not implement any other interface. It can only extend it.

接口可能不实现任何其他接口。它只能扩展它。

#3


6  

ad 1. It does not implement its methods.

广告1.它没有实现其方法。

ad 4. The purpose of one interface extending, not implementing another, is to build a more specific interface. For example, SortedMap is an interface that extends Map. A client not interested in the sorting aspect can code against Map and handle all the instances of for example TreeMap, which implements SortedMap. At the same time, another client interested in the sorted aspect can use those same instances through the SortedMap interface.

ad 4.扩展一个接口而不是另一个接口的目的是构建一个更具体的接口。例如,SortedMap是一个扩展Map的接口。对排序方面不感兴趣的客户端可以对Map进行编码并处理例如实现SortedMap的TreeMap的所有实例。同时,对排序方面感兴趣的另一个客户端可以通过SortedMap接口使用这些相同的实例。

In your example you are repeating the methods from the superinterface. While legal, it's unnecessary and doesn't change anything in the end result. The compiled code will be exactly the same whether these methods are there or not. Whatever Eclipse's hover says is irrelevant to the basic truth that an interface does not implement anything.

在您的示例中,您将从超级接口重复这些方法。虽然合法,但它是不必要的,并且不会改变最终结果中的任何内容。无论这些方法是否存在,编译后的代码都完全相同。无论Eclipse的悬停说什么都与接口没有实现任何东西的基本事实无关。