是否应该使用或不使用公共访问修饰符来声明Java接口中的方法? [英] Should methods in a Java interface be declared with or without a public access modifier?

查看:188
本文介绍了是否应该使用或不使用公共访问修饰符来声明Java接口中的方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果使用或不使用 public 访问修饰符来声明Java接口中的方法?

Should methods in a Java interface be declared with or without the public access modifier?

当然,。实现接口的类方法总是 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本身不一致。请参阅集合比较未来 vs 。 ScriptEngine

Java itself is not consistent in this. See for instance Collection vs. Comparable, or Future vs. ScriptEngine.

推荐答案

JLS 说明了这一点:


这是允许的,但不鼓励作为风格,冗余地指定 public 和/或 abstract 在接口中声明的方法的修饰符。

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.

这篇关于是否应该使用或不使用公共访问修饰符来声明Java接口中的方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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