本文介绍了Java 8中接口方法的隐式声明是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在读我的旧SCJP 6书(作者Kathy Sierra,Bert Bates)提到

I was reading my old SCJP 6 book(author Kathy Sierra,Bert Bates) mentioned


  • 所有接口默认情况下隐式 public abstract / li>
  • interface 方法不能 static

  • All the interface methods are implicitly public and abstract by default
  • interface methods must not be static

例如,如果我们声明

interface Car
{
    void bounce();               //no need of public abstract
    void setBounceFactor(int b); //no need of public abstract
}

编译器看到了什么

interface Car
{
    public abstract void bounce();
    public abstract void setBounceFactor(int b);
}

但是来自Java 8的 ,接口现在可以定义静态方法。

我的问题是,Java 8中接口方法的隐式声明是什么?只有 public 或什么都没有?

But from Java 8, interfaces can now define static methods. see this article everything-about-java-8
My question, what is the implicit declaration of interface methods in Java 8? Only public or nothing?

推荐答案

隐式规则修饰符不会更改。未指定其他修饰符时使用隐式修饰符。如果未指定 static 默认,则隐含 abstract 。并且所有方法总是 public 无论是隐式还是显式。请注意, interface 字段始终隐式 public static 。这也不会改变。

The rules for implicit modifiers do not change. Implicit modifiers are used when no other modifiers are specified. abstract is implied when neither static nor default has been specified. And all methods are always public whether implicit or explicit. Note that interface fields were always implicitly public static. This doesn’t change too.

但最后的话我们应该等待Java 8的完成。

But for the final words we should wait for the completion of Java 8.

这篇关于Java 8中接口方法的隐式声明是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-22 14:29