是否应该使用或不使用公共访问修饰符来声明 Java 接口中的方法? [英] Should methods in a Java interface be declared with or without a public access modifier?
问题描述
Java 接口中的方法是否应该使用public
访问修饰符来声明?
Should methods in a Java interface be declared with or without the public
access modifier?
当然,从技术上讲,这无关紧要.实现interface
的类方法总是public
.但什么是更好的约定?
Technically it doesn't matter, of course. A class method that implements an interface
is always public
. But what is a better convention?
Java 本身在这方面并不一致.参见例如Collection
vs. Comparable
,或Future
vs. ScriptEngine
.
Java itself is not consistent in this. See for instance Collection
vs. Comparable
, or Future
vs. ScriptEngine
.
推荐答案
JLS 清楚地说明了这一点:
The JLS makes this clear:
允许但不鼓励为接口中声明的方法冗余指定 public
和/或 abstract
修饰符.
It is permitted, but discouraged as a matter of style, to redundantly specify the
public
and/orabstract
modifier for a method declared in an interface.
这篇关于是否应该使用或不使用公共访问修饰符来声明 Java 接口中的方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!