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

标签 java interface default

我一直在阅读有关在 Java 中使用 @Override 的一些问题。 (例如 this one on overridethis 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?

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

关于java - @Override,在接口(interface)中使用默认方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34117836/

相关文章:

java - 我可以使用 OpenGL-ES glBlendFunc 来影响混合以将目标考虑在内吗?

Typescript - 如何访问通过命名空间声明的类?

java - 使用 Java 创建安全 Websocket (wss) 服务器

java - 如何在 onClick 事件触发时更改 Fragment?

c# - 使用类型参数约束实现两次相同的通用接口(interface)

java - 如果消除了对多重继承的限制,那么 Java 中的接口(interface)和抽象类有什么区别?

c++ - 在 C++ 中创建一个类

android - 有没有简单的方法可以在 Android 中为 TextView 创建用户默认值?

mysql - 无法更改表,出现错误 1067 无效的默认值

java - Guava Stopwatch类有什么优势?