java - @Override,在接口(interface)中使用默认方法

我一直在阅读有关在 Java 中使用 @Override 的一些问题。 (例如 this one on override 和 this one on default methods ,显然是 documentations )但是,我仍然很困惑。

当接口(interface)中的所有行为都需要由类使用时,我被教导要始终使用和实现接口(interface)。我明白了。但是正如我被教导的那样,你会做这样的事情(部分取自文档):

public interface TimeClient {
    void setTime(int hour, int minute, int second);
}

然后由类实现。

public class TestSimpleTimeClient implements TimeClient {
    public static void main(String[] args) {
    }

    @Override
    public void setTime(int hour, int minute, int second) {
         System.out.println(hour + " " + minute + " " +second);
    }
}

让我烦恼的是接口(interface)中方法的实现。它什么都不做,它只是被声明为一个接受参数但不做任何其他事情的方法。然后您采用该方法并在实现该接口(interface)的类中重写它。

我知道这是一种“强制”类实现方法的方法,但我看不出这在某些特定用例中有何用处。

假设我有一个由几个类实现的接口(interface)。我希望这些类中的大多数共享方法的相同实现,但不是全部。合乎逻辑且符合字符效率的方法是有一种说法:这些类采用接口(interface)中的默认方法,但这些类覆盖了默认方法。我该怎么做呢?覆盖该方法的方法是否应该仅实现它,而仅将默认方法作为一个整体使用的方法是否应该扩展它?如果您只希望接口(interface)中的特定方法具有这种行为怎么办?

最佳答案

The thing that bugs me is the implementation of the method in the interface. It doesn't do anything, it's only declared as a method that take arguments but doesn't do anything else.

那不是“接口(interface)中方法的实现”。那只是一个接口(interface)方法声明。在编程中,术语很重要。接口(interface)往往没有任何实现。 (除非你在谈论 Java 8 的默认接口(interface)方法,但从你的问题的其余部分来看,不清楚你是否知道它们的存在。)

I understand that this is a way to "force" classes to implement a class

一个类不能实现一个类。一个类扩展了一个类。但是一个类实现了一个接口(interface)。在编程中,术语很重要。

这不仅仅是一种强制类提供实现的方法。这也是调用者能够调用接口(interface)方法而无需了解实现它的类的一种方式。

but I don't see how this is useful in some specific use cases.

好吧,以 Collection<T> 为例接口(interface),以及 contains()方法,由无数类实现,其中ArrayList<T> , LinkedList<T> , HashSet<T> , BoundedBlockingQueue<T> , 等等等等。您的代码可能如下所示:

boolean hasPekingese( Collection<Animal> animals )
{
    return animals.contains( AllOfMyAnimals.PEKINGESE );
}

请注意 hasPekingese()方法不必知道正在实现 Collection<Animal> 的确切类.这意味着您可以调用 hasPekingese()来自将其动物饲养在ArrayList<Animal>中的类, 你也可以调用 hasPekingese()来自将其动物饲养在BoundedBlockingQueue<Animal>中的类.方法hasPekingese()不知道,也不关心。

Let's say I have an interface that's shared by a couple of classes.

不清楚您所说的“共享”是什么意思。您指的是 "implemented"吗?在编程中,术语至关重要。

I want most of these classes to share the same implementation of the method, but not all. The logical, and character-efficient way would be to have a way to say: these classes take the default method in the interface, but these classes override the default method. How would I go about doing that?

有很多方法可以做到这一点,最常见的是让这些类中的一些扩展一些公共(public)基类,它提供了你的方法的公共(public)实现,这样派生类就继承了这个方法,所以它们不会必须实现它。其余类不扩展该公共(public)基类,因此它们中的每一个都必须提供该方法的自己的实现。

Should the one that overrides the method only implement it, whereas the ones that simply use the default method as a whole extend it?

这还不清楚。另外,请不要称它为“默认方法”,因为从 Java 8 开始,“默认方法”是一个具有非常特定含义的术语,尽管它与本次讨论相关,但与您所理解的意义不同正在使用它。

And what if you only want this behaviour for a specific method in an interface?

如果派生类希望该方法以不同方式工作,它可能会重新覆盖它。或者,您可能有两个不同的基类,一个以某种方式实现该方法,另一个以不同的方式实现它,因此您的派生类中有一半扩展了第一个基类,而另一半派生类扩展了第二个基类。

https://stackoverflow.com/questions/34117836/

相关文章:

sql - 带 Where 子句的 OFFSET SQL 查询

matrix - Spark RDD 到 Matrix

types - 如何声明 TypeScript 导入 * 类型?

sql-server - 在 SQL Server 2012 的情况下,尝试在我的 select 语

android - 谷歌身份验证后,即将到来的网址不允许我在 Cordova 应用程序中走得更远

d3.js - d3 中的自定义比例

java - 收听 RabbitMQ 队列并获取事件通知

android - 带有 Snackbar 和透明导航栏的 FloatingActionButton

python - 使用 python 3.5 用希伯来语和英语解析 json 文件

java - 在 Apache Spark SQL 中对多行进行操作