Java 接口方法可以有主体吗?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/22713652/
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
Can an interface method have a body?
提问by
I know that an interface is like a 100% pure abstract class. So, it can't have method implementation in it. But, I saw a strange code. Can anyone explain it?
我知道接口就像一个 100% 纯抽象类。所以,它不能有方法实现。但是,我看到了一个奇怪的代码。谁能解释一下?
Code Snippet:
代码片段:
interface Whoa {
public static void doStuff() {
System.out.println("This is not default implementation");
}
}
EDIT:
编辑:
My IDE is Intellij Idea 13.1. The project SDK is java 7 <1.7.0_25>. The IDE is not showing any compiler error. But, When I compile the code at command line I am getting the following message.
我的 IDE 是 Intellij Idea 13.1。项目 SDK 为 java 7 <1.7.0_25>。IDE 没有显示任何编译器错误。但是,当我在命令行编译代码时,我收到以下消息。
Whoa.java:2: error: modifier static not allowed here public static void doStuff() { ^
Whoa.java:2: error: modifier static not allowed here public static void doStuff() { ^
采纳答案by Aniket Kulkarni
From Java 8you can define static methods in interfaces in addition to default methods.
从Java 8 开始,除了默认方法之外,您还可以在接口中定义静态方法。
A static method is a method that is associated with the class in which it is defined rather than with any object. Every instance of the class shares its static methods.
This makes it easier for you to organize helper methods in your libraries; you can keep static methods specific to an interface in the same interface rather than in a separate class.
The following example defines a static method that retrieves a
ZoneId
object corresponding to a time zone identifier; it uses the system default time zone if there is noZoneId
object corresponding to the given identifier. (As a result, you can simplify the methodgetZonedDateTime
)
静态方法是与定义它的类相关联的方法,而不是与任何对象相关联的方法。类的每个实例共享其静态方法。
这使您可以更轻松地在库中组织辅助方法;您可以将特定于接口的静态方法保留在同一个接口中,而不是在单独的类中。
以下示例定义了一个静态方法,该方法检索
ZoneId
与时区标识符对应的对象;如果没有ZoneId
与给定标识符对应的对象,则使用系统默认时区。(因此,您可以简化方法getZonedDateTime
)
Here is code :
这是代码:
public interface TimeClient {
// ...
static public ZoneId getZoneId (String zoneString) {
try {
return ZoneId.of(zoneString);
} catch (DateTimeException e) {
System.err.println("Invalid time zone: " + zoneString +"; using default time zone instead.");
return ZoneId.systemDefault();
}
}
default public ZonedDateTime getZonedDateTime(String zoneString) {
return ZonedDateTime.of(getLocalDateTime(), getZoneId(zoneString));
}
}
See also
也可以看看
For all interesting things in Java 8 read Everything about Java 8
有关 Java 8 中所有有趣的内容,请阅读有关 Java 8 的所有内容
回答by The Guy with The Hat
This is only possible in Java 8. In the Java 7Language Specification §9.4, it explicitly states:
这只能在 Java 8 中实现。在Java 7语言规范 §9.4 中,它明确指出:
It is a compile-time error if a method declared in an interface is declared static, because static methods cannot be abstract.
如果在接口中声明的方法声明为静态,则会导致编译时错误,因为静态方法不能是抽象的。
So in Java 7, static methods in interfaces cannotexist.
所以在 Java 7 中,接口中的静态方法不能存在。
If you go to the Java 8Language Specification §9.4.3, you can see that it says:
如果您转到Java 8语言规范 §9.4.3,您可以看到它说:
A static method also has a block body, which provides the implementation of the method.
静态方法也有一个块体,它提供了方法的实现。
So it explicitly states that in Java 8, they canexist.
所以它明确指出在 Java 8 中,它们可以存在。
I even tried to run your exact code in Java 1.7.0_45, but it gave me the error "modifier static not allowed here".
我什至尝试在 Java 1.7.0_45 中运行您的确切代码,但它给了我错误“此处不允许使用修饰符静态”。
Here is a quote directly from the Java 8tutorial, Default Methods (Learning the Java Language > Interfaces and Inheritance):
以下是直接来自 Java 8教程默认方法(学习 Java 语言 > 接口和继承)的引述:
Static Methods
In addition to default methods, you can define static methodsin interfaces. (A static method is a method that is associated with the class in which it is defined rather than with any object. Every instance of the class shares its static methods.) This makes it easier for you to organize helper methods in your libraries; you can keep static methods specific to an interface in the same interface rather than in a separate class. The following example defines a static method that retrieves a
ZoneId
object corresponding to a time zone identifier; it uses the system default time zone if there is noZoneId
object corresponding to the given identifier. (As a result, you can simplify the methodgetZonedDateTime
):public interface TimeClient { // ... static public ZoneId getZoneId (String zoneString) { try { return ZoneId.of(zoneString); } catch (DateTimeException e) { System.err.println("Invalid time zone: " + zoneString + "; using default time zone instead."); return ZoneId.systemDefault(); } } default public ZonedDateTime getZonedDateTime(String zoneString) { return ZonedDateTime.of(getLocalDateTime(), getZoneId(zoneString)); } }
Like static methods in classes, you specify that a method definition in an interface is a static method with the
static
keyword at the beginning of the method signature. All method declarations in an interface, including static methods, are implicitlypublic
, so you can omit thepublic
modifier.
静态方法
除了默认方法之外,您还可以在接口中定义静态方法。(静态方法是与定义它的类相关联的方法,而不是与任何对象相关联的方法。类的每个实例共享其静态方法。)这使您可以更轻松地在库中组织辅助方法;您可以将特定于接口的静态方法保留在同一个接口中,而不是在单独的类中。以下示例定义了一个静态方法,该方法检索
ZoneId
与时区标识符对应的对象;如果没有ZoneId
与给定标识符对应的对象,则使用系统默认时区 。(因此,您可以简化方法getZonedDateTime
):public interface TimeClient { // ... static public ZoneId getZoneId (String zoneString) { try { return ZoneId.of(zoneString); } catch (DateTimeException e) { System.err.println("Invalid time zone: " + zoneString + "; using default time zone instead."); return ZoneId.systemDefault(); } } default public ZonedDateTime getZonedDateTime(String zoneString) { return ZonedDateTime.of(getLocalDateTime(), getZoneId(zoneString)); } }
与类中的静态方法一样,您可以
static
在方法签名的开头使用关键字指定接口中的方法定义是静态方法。接口中的所有方法声明,包括静态方法,都是隐式的public
,因此您可以省略public
修饰符。
回答by gstackoverflow
For javaversion 7 or below, similar functionally you can achieve using nested class declared within interface body. and this nested class implements outer interface.
对于java 7 或以下版本,类似的功能您可以使用在接口主体中声明的嵌套类来实现。这个嵌套类实现了外部接口。
Example:
例子:
interface I1{
public void doSmth();
class DefaultRealizationClass implements I1{
@Override
public void doSmth() {
System.out.println("default realization");
}
}
}
How do we use it in our code?
我们如何在我们的代码中使用它?
class MyClass implements I1{
@Override
public void doSmth() {
new I1.DefaultRealizationClass().doSmth();
}
}
Therefore default implementation encapsulated within interface.
因此默认实现封装在接口中。