java接口中的静态方法 [英] Static methods in java interface

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

问题描述

据我所知,你不能在接口体中声明静态方法。但是,我偶然在 http://docs.oracle.com/ 网站上找到了一些特殊的代码。这是链接

As far as I know you cannot declare static methods in interface body. However, accidentally I found peculiar piece of code on http://docs.oracle.com/ site. Here is the link

Namelly

public interface TimeClient 
{
void setTime(int hour, int minute, int second);
void setDate(int day, int month, int year);
void setDateAndTime(int day, int month, int year,
                           int hour, int minute, int second);
LocalDateTime getLocalDateTime();

static 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 ZonedDateTime getZonedDateTime(String zoneString) {
    return ZonedDateTime.of(getLocalDateTime(), getZoneId(zoneString));
    }
}

界面静态方法 getZoneId

我迷路了......任何人都可以解释

I am lost... could anyone explain please

推荐答案

Java 8现在有了接口中默认方法实现的想法:

Java 8 now has the idea of "default" method implementations in interfaces:

http://blog.hartveld.com/2013/03/jdk-8-13-interface-default-method.html

这篇关于java接口中的静态方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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