java接口中的静态方法

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/23148471/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 21:03:32  来源:igfitidea点击:

Static methods in java interface

javainterfacestatic-methods

提问by Russell'sTeapot

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

据我所知,您不能在接口主体中声明静态方法。但是,我无意中在http://docs.oracle.com/站点上发现了一段奇怪的代码。这是链接

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));
    }
}

this interfacehas a staticmethod getZoneId

interface有一个static方法getZoneId

I am lost... could anyone explain please

我迷路了……谁能解释一下

采纳答案by Sean Owen

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

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

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

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

回答by Jason C

Starting with Java 8 you can do this. The official tutorialthat your snippet came from (which has been updated for Java 8) says it best:

从 Java 8 开始,您可以执行此操作。您的代码段来自的官方教程(已针对 Java 8 进行了更新)说得最好:

The interface body can contain abstract methods, default methods, and static methods. An abstract method within an interface is followed by a semicolon, but no braces (an abstract method does not contain an implementation). Default methods are defined with the defaultmodifier, and static methods with the statickeyword. All abstract, default, and static methods in an interface are implicitly public, so you can omit the public modifier.

接口主体可以包含抽象方法、默认方法和静态方法。接口中的抽象方法后跟一个分号,但没有大括号(抽象方法不包含实现)。默认方法用default修饰符定义,静态方法用static关键字定义。接口中的所有抽象、默认和静态方法都是隐式公开的,因此您可以省略 public 修饰符。

Or from Java 8's JLS section 9.4:

或者来自 Java 8 的JLS 第 9.4 节

A default method is a method that is declared in an interface with the defaultmodifier; its body is always represented by a block. It provides a default implementation for any class that implements the interface without overriding the method. Default methods are distinct from concrete methods, which are declared in classes.

...

An interface can declare static methods, which are invoked without reference to a particular object.

默认方法是在带有default修饰符的接口中声明的方法;它的主体总是由一个块表示。它为实现接口而不覆盖方法的任何类提供默认实现。默认方法不同于在类中声明的具体方法。

...

接口可以声明静态方法,这些方法在不引用特定对象的情况下被调用。

回答by Sanjay Rabari

in Java 8, interfaces can have static methods, as well as override-able methods with a default implementation. They still can't have instance fields though. These features are part of the lambda expression support, and you can read more about them in Part H of.JSR 335

在 Java 8 中,接口可以有静态方法,也可以有默认实现的可重写方法。他们仍然不能有实例字段。这些特性是 lambda 表达式支持的一部分,你可以在 H 部分阅读更多关于它们的信息。JSR 335

回答by Ivan Babanin

From Java Language Specification (Java SE 8 Edition): http://docs.oracle.com/javase/specs/jls/se8/html/jls-9.html#jls-9.4

来自 Java 语言规范(Java SE 8 版):http: //docs.oracle.com/javase/specs/jls/se8/html/jls-9.html#jls-9.4

An interface can declare static methods, which are invoked without reference to a particular object.

接口可以声明静态方法,这些方法在不引用特定对象的情况下被调用。

回答by fge

You are the witness of two new features in Java 8 here:

您是 Java 8 中两个新特性的见证:

  • static methods in interfaces,
  • virtual extension methods.
  • 接口中的静态方法,
  • 虚拟扩展方法。

In the code sample you provide, getZoneId()illustrates the first novelty, and .getZoneDateTime()the second one.

在您提供的代码示例中,getZoneId()说明了第一个新颖性和.getZoneDateTime()第二个新颖性。

The second one in particular is what has allowed the Collectioninterface to be extended with supplementary methods such as .stream(), for example, withoutbreaking backwards compatibility. See herefor an illustration.

尤其是第二个是允许Collection使用补充方法扩展接口.stream(),例如,在破坏向后兼容性的情况下。请参见此处的说明。

The first one allows to avoid writing "method bags" classes which often exist only to provide utility static methods over interfaces. One such example would be Guava's Functionsclass(not to be mixed with Java 8's Functionwhich it basically stole from Guava, along with Predicateand a few others)

第一个允许避免编写通常仅用于通过接口提供实用程序静态方法的“方法包”类。一个这样的例子是Guava 的Functions(不要与Function它基本上从 Guava 以及Predicate其他一些人那里偷来的Java 8 混合在一起)