欢迎来到 Android人的问与答 ,在这里提问或帮助他人解答。 Question2Answer的1.6.x和1.7.x版本的中文翻译包我修改更新:

Question2answer

如何使用中文语言包

Q2A 1.6.x - Chinese Simplified (简体中文)

Q2A 1.7.x - Chinese Simplified (简体中文)


国内服务器推荐 阿里云服务器
国外服务器推荐 DigitalOcean
VPN推荐 正版Green VPN
阿里云推荐码:ldvq50

Java 中 modifer 'public' is reduntant for interface methods

0 喜欢 0 不喜欢
329 浏览

经常会看到接口上写了public修饰方法,然后IDE,比如:Eclipse或者IDEA经常会提示public是多余的。后来就查了一下。java默认接口的方法是public和abstract的,所以真没必要。同时,如果你使用private或者protected都会报错。

所以,安心删除public吧。

http://stackoverflow.com/questions/17011374/are-public-and-public-final-redundant-for-interface-methods

You cannot have a final method declared in an interface. Fields are always final but methods are always abstract (and never final). You cannot define an interface method that is to be implemented only by classes in the same package.* From section 9.3 of the Java Language Specification:

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.

and from section 9.4:

Every method declaration in the body of an interface is implicitly public (§6.6).

Every method declaration in the body of an interface is implicitly abstract, so its body is always represented by a semicolon, not a block.

It is permitted, but discouraged as a matter of style, to redundantly specify the public and/or abstract modifier for a method declared in an interface.

* As Paul Bellora points out in a comment, you can make the interface itself package-private (or protected, or even private) if you want to restrict its visibility.

最新提问 6月 12, 2015 分类:Java基础 | 用户: forlong401 (7,050 分)

登录 或者 注册 后回答这个问题。

...