gpt4 book ai didi

Java - 接口(interface)、方法需要返回一些东西吗?

转载 作者:行者123 更新时间:2023-12-02 22:59:18 30 4
gpt4 key购买 nike

(第一篇文章不要欺负我:D)

我的问题很简单,接口(interface)中包含的方法是否必须返回某个值? (整数、 double 、字符串等..)因为上次我检查时无法在接口(interface)中定义 Void 方法,出现编译错误。

提前致谢!干杯!

最佳答案

My question is simple, is it imperative that a method included in an interface HAS to return some value?

不,绝对不是。

您可以在接口(interface)中声明 void 方法,实际上有很多标准库接口(interface)都包含此类方法。 Runnable 是一个很好的例子:

public interface Runnable() {
void run();
}

请注意,声明一个方法返回 Void则是另一回事,并且通常是一个错误。 (它主要对于要返回 T 类型值的泛型方法有用 - 例如, Runnable 类似于 Callable<Void> 。)

关于Java - 接口(interface)、方法需要返回一些东西吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19766338/

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