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

问题描述

我知道接口方法是隐式地public. Java文档教程

I understand that Interface methods are implicitly public. Java Docs Tutorial says

出于抽象,静态和默认的考虑,这些修饰符在Java 8中是隐式声明的.

Out of abstract, static and default, which modifiers are implicitly declared in Java 8.

我认为随着Java 8在接口中引入default方法,不再隐式声明abstract了,但是我仍然在Intellij IDEA中得到警告.

I thought abstract was not implicitly declared anymore as Java 8 introduced default method in interfaces, but I still get a warning in Intellij IDEA.

public interface TestInterface {
    abstract int print();  // abstract redundant ?.
    int print2(); //legal.but public or public abstract ?.
}

推荐答案

语言规范-特别是9.4节,指出abstractpublic是隐式的.

The language spec - specifically Section 9.4, states that abstract and public are implicit.

缺少默认修饰符或静态修饰符的接口方法是隐式抽象的,因此其主体由分号而不是块表示.允许(但不鼓励使用样式)为这种方法声明多余地指定abstract修饰符.

An interface method lacking a default modifier or a static modifier is implicitly abstract, so its body is represented by a semicolon, not a block. It is permitted, but discouraged as a matter of style, to redundantly specify the abstract modifier for such a method declaration.

这就是IntelliJ向您发出警告的原因;通过JLS,您正在做的事情完全多余.

This is why IntelliJ warns you about it; by the JLS, you're doing something completely redundant.

作为奖励,接口隐式为public static final:

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

09-24 18:55