是否有必要将方法定义为“公共”?

发布于 2024-09-19 19:30:30 字数 67 浏览 4 评论 0原文

如果所有方法都是公共的,除非它们被明确定义为其他方法,那么是否有必要将方法定义为public

If all methods are public unless they are explicitly defined as something else, is it ever necessary to define a method as public?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

你的往事 2024-09-26 19:30:30

好吧,您已经通过命名默认值回答了问题:不,没有必要。

引用手册

类方法可以定义为公共、私有或受保护。没有任何显式可见性关键字声明的方法被定义为公共。

不过,我认为始终这样做是一种很好的做法。

Well, you answered the question already by naming the default: no, it is not necessary.

To quote the manual:

Class methods may be defined as public, private, or protected. Methods declared without any explicit visibility keyword are defined as public.

However, I consider it good practise to always do so.

昔日梦未散 2024-09-26 19:30:30

因为 public 是默认值,因此没有必要定义它。

Because public is the default it isn't necessary to define it.

2024-09-26 19:30:30

php 有两种风格倾向 - “javaesque”大多数人认为可见性修饰符非常重要并积极使用它们,而在 pythonesque 少数派(我个人属于)中,我们认为所有公共-私有-接口-抽象的东西只不过是浪费内存。

There are two style tendencies in php - the "javaesque" majority considers visibility modifiers very important and uses them actively, in the pythonesque minority (i personally belong to) we think that all that public-private-interface-abstract stuff is nothing more but a waste of RAM.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文