gpt4 book ai didi

java - 如何在实现接口(interface)后在另一个接口(interface)上强制执行类型

转载 作者:塔克拉玛干 更新时间:2023-11-02 08:35:03 26 4
gpt4 key购买 nike

我想创建一个强制执行类型的自定义函数。

public interface StringGroupFunction implements Function<String, String> {
}

这是不允许的。我发现的唯一可能性是使 StringGroupFunction 成为抽象类。还有其他想法吗?

最佳答案

这是一个常见的误解:接口(interface)不实现另一个接口(interface),它扩展它,因为它不为函数提供主体。

doc 中所述:

If you want to add additional methods to an interface, you have several options. You could create a DoItPlus interface that extends DoIt:

public interface DoItPlus extends DoIt {
boolean didItWork(int i, double x, String s);
}

您可以在 java specification 中阅读有关接口(interface)的更多信息:

If an extends clause is provided, then the interface being declared extends each of the other named interfaces and therefore inherits the member types, methods, and constants of each of the other named interfaces.

These other named interfaces are the direct superinterfaces of the interface being declared.

Any class that implements the declared interface is also considered to implement all the interfaces that this interface extends.

关于java - 如何在实现接口(interface)后在另一个接口(interface)上强制执行类型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44516088/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com