Java 8中接口方法的隐式说明符/修饰符是什么? [英] What are the implicit specifier/modifiers of interface methods in Java 8?

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

问题描述

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

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

接口中的所有abstractdefaultstatic方法是 隐式地public,因此您可以省略public修饰符.

All abstract, default, and static methods in an interface are implicitly public, so you can omit the public modifier.

出于抽象,静态和默认的考虑,这些修饰符在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.

修饰符"abstract"对于接口方法是多余的.

Modifier 'abstract' is redundant for interface methods.

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

推荐答案

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

接口主体中的每个方法声明都是隐式公共的(第6.6节).允许但出于风格考虑不鼓励在接口中为方法声明多余地指定public修饰符.

Every method declaration in the body of an interface is implicitly public (§6.6). It is permitted, but discouraged as a matter of style, to redundantly specify the public modifier for a method declaration in an interface.

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

接口主体中的每个字段声明都是隐式的public,static和final.可以为此类字段多余地指定任何或所有这些修饰符.

Every field declaration in the body of an interface is implicitly public, static, and final. It is permitted to redundantly specify any or all of these modifiers for such fields.

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

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