接口方法可以有主体吗? [英] Can an interface method have a body?

查看:28
本文介绍了接口方法可以有主体吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道接口就像一个 100% 纯抽象类.所以,它不能有方法实现.但是,我看到了一个奇怪的代码.谁能解释一下?

代码片段:

 interface 哇 {公共静态无效 doStuff() {System.out.println("这不是默认实现");}}

我的 IDE 是 Intellij Idea 13.1.项目 SDK 是 java 7 <1.7.0_25>.IDE 没有显示任何编译器错误.但是,当我在命令行编译代码时,我收到以下消息.

<块引用>

Whoa.java:2: 错误:此处不允许使用修饰符 static公共静态无效 doStuff() {^

解决方案

Java 8 开始,除了默认方法之外,您还可以在接口中定义静态方法.

  • 静态方法是与定义它的类而非任何对象相关联的方法.该类的每个实例都共享其静态方法.

  • 这使您可以更轻松地组织库中的辅助方法;您可以将特定于接口的静态方法保留在同一个接口中,而不是在单独的类中.

  • 以下示例定义了一个静态方法,用于检索与时区标识符对应的 ZoneId 对象;如果没有与给定标识符对应的 ZoneId 对象,则它使用系统默认时区.(因此,您可以简化方法 getZonedDateTime)

这是代码:

公共接口 TimeClient {//...静态公共 ZoneId getZoneId (String zoneString) {尝试 {返回 ZoneId.of(zoneString);} catch (DateTimeException e) {System.err.println("无效时区:" + zoneString +"; 使用默认时区代替.");返回 ZoneId.systemDefault();}}默认 public ZonedDateTime getZonedDateTime(String zoneString) {return ZonedDateTime.of(getLocalDateTime(), getZoneId(zoneString));}}

另见

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?

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.

Whoa.java:2: error: modifier static not allowed here
    public static void doStuff() {
                       ^

解决方案

From Java 8 you can define static methods in interfaces in addition to default methods.

  • 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 no ZoneId object corresponding to the given identifier. (As a result, you can simplify the method 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

这篇关于接口方法可以有主体吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆