[英]Java - Interfaces, methods need to return something?
(1st post don't bully me :D) (第一帖不要欺负我:D)
My question is simple, is it imperative that a method included in an interface HAS to return some value? 我的问题很简单,是否必须让接口中包含的方法必须返回某些值? (int, double, String etc..) Cause last time I checked I could not define a Void method in an interface, got compiling errors.
(int,double,String等。)原因上次我检查无法在接口中定义Void方法时,出现编译错误。
Thanks in advance! 提前致谢! Cheers!
干杯!
My question is simple, is it imperative that a method included in an interface HAS to return some value?
我的问题很简单,是否必须让接口中包含的方法必须返回某些值?
No, absolutely not. 不,绝对不是。
You can declare a void method in an interface, and indeed there are plenty of standard library interfaces with such methods. 您可以在接口中声明一个void方法,并且确实有很多标准库接口都带有此类方法。
Runnable
is a fine example: Runnable
是一个很好的例子:
public interface Runnable() {
void run();
}
Note that declaring that a method returns Void
is a different matter, and usually a mistake. 注意,声明方法返回
Void
是另一回事, 通常是一个错误。 (It's primarily useful for generic methods where you're going to return a value of type T
- for example, Runnable
is similar to Callable<Void>
.) (这对于将要返回
T
类型值的泛型方法主要有用-例如, Runnable
与Callable<Void>
相似。)
Did you use "Void" or "void"? 您使用“无效”还是“无效”? (Use "void"!)
(使用“ void”!)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.