Java 接口中的默认方法返回值
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3821143/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Default method return value in Java interfaces
提问by MicSim
While working with annotations I stumbled accross the following piece of code (it's the Hibernate @NotNull annotation):
在使用注释时,我偶然发现了以下代码段(它是 Hibernate @NotNull 注释):
@Target(value = {ElementType.METHOD, ElementType.FIELD, ElementType.ANNOTATION_TYPE, ElementType.CONSTRUCTOR, ElementType.PARAMETER})
@Retention(value = RetentionPolicy.RUNTIME)
@Documented
@Constraint(validatedBy = {})
public @interface NotNull {
@Target(value = {ElementType.METHOD, ElementType.FIELD, ElementType.ANNOTATION_TYPE, ElementType.CONSTRUCTOR, ElementType.PARAMETER})
@Retention(value = RetentionPolicy.RUNTIME)
@Documented
public @interface List {
public NotNull[] value();
}
public String message() default "{javax.validation.constraints.NotNull.message}";
public Class<?>[] groups() default {};
public Class<? extends Payload>[] payload() default {};
}
I was wondering about the default
keyword/construct in the method definition, which I've never seen before. As I understood, it lets you define a default value for this method (or annotation property).
我想知道default
方法定义中的关键字/结构,这是我以前从未见过的。据我了解,它允许您为此方法(或注释属性)定义默认值。
Now I was trying to apply this construct to a normal interface, but it failed. This would fail to compile:
现在我试图将此构造应用于普通接口,但它失败了。这将无法编译:
public interface DefaultTest {
public String test() default "value";
}
But this would work:
但这会起作用:
public @interface DefaultTest {
public String test() default "value";
}
So my question is: Is the default
keyword annotation-specific? And if yes, what speaks against using this construct in normal interface definitions?
所以我的问题是:是的default
关键字注释的具体情况?如果是的话,什么反对在普通接口定义中使用这个构造?
采纳答案by Pace
Java 8 and onwards
Java 8 及更高版本
Yes, the default keyword can now be used on interfaces. For more details see Oracle's docs.
是的,现在可以在接口上使用 default 关键字。有关更多详细信息,请参阅Oracle 的文档。
The implementation is slightly different than what you were thinking. It would be:
实现与您的想法略有不同。这将是:
public interface DefaultTest {
default public String test() {
return "value";
}
}
Java 7 and previous
Java 7 及更早版本
The default keyword can only be used for annotations.
default 关键字只能用于注释。
If you want a default return value for an interface you need to use an abstract class instead.
如果你想要一个接口的默认返回值,你需要使用抽象类。
public abstract class DefaultTest {
public String test() {
return "value";
}
}
回答by gtm.r
The 'default' keyword here is not annotation specific. Its the feature of java to provide default implementation to interface methods.
这里的“默认”关键字不是特定于注释的。它是java的特性,为接口方法提供默认实现。
Need of this behaviour :Suppose initially a interface Vehicle was defined to support all vehicle functional methods -
这种行为的需要:假设最初定义了一个接口 Vehicle 来支持所有车辆功能方法 -
interface Vehicle {
void speed();
...
//other interface methods
}
Now classes implementing this Vehicle interface have implemented those abstract methods.
现在实现这个 Vehicle 接口的类已经实现了这些抽象方法。
Now in future Vehicle have capability of flying. So you need to add flying feature as well. Now if you add flyingSpeed() method to Vehicle interface, you need to modify all the existing classes to avoid break the code. Not feasible solution.
现在在未来车辆具有飞行能力。所以你还需要添加飞行功能。现在,如果您在 Vehicle 接口中添加 flySpeed() 方法,则需要修改所有现有类以避免破坏代码。不可行的解决方案。
For backward compatibility java has provided the feature of Default method. So that you can add new methods to interface with default implementation, so the existing classes would not need to implement that method. And the new Vehicle classes can override those methods as per their need.
为了向后兼容,java 提供了 Default 方法的特性。这样您就可以添加新方法以与默认实现接口,因此现有类不需要实现该方法。新的 Vehicle 类可以根据需要覆盖这些方法。
interface Vehicle {
void speed();
...
//other interface methods
//default method
default void flyingSpeed() {
System.out.println("Default flying speed");
}
}
Using this way previous existing classes of Vehicle wouldn't need to implement this method.
使用这种方式以前现有的 Vehicle 类不需要实现这个方法。
For more information seehere.
有关更多信息,请参见此处。