Java中public int和int有什么区别?

发布于 2025-01-08 04:22:54 字数 174 浏览 4 评论 0原文

我才开始学习 Java(我是一名学生,C++ 是我最了解的基础语言)并且我遇到了访问和修饰符。

在代码中我看到了使用

 public int NAME;
 // and
 int NAME;

它有什么区别吗?如果是这样,有什么区别。它到底有什么作用?

I've only started to learn Java (I'm a student, and C++ is the base langauge I know best) and I've come across access and modifiers.

In code I've seen the use of

 public int NAME;
 // and
 int NAME;

Does it make a difference? If so, what is the difference. What does it do exactly?

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

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

发布评论

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

评论(1

挽袖吟 2025-01-15 04:22:54

public int 是一个没有访问控制的变量。它实际上是一个全局变量。如果您愿意的话,未修改的 int 是“受保护的+” int,它作为受保护的 int 运行,但不能被子类使用。受保护的 int 可以由包含该特定 int 变量的类的子类使用。私有整数只能在该类中使用。通常建议使用尽可能少的公共变量,以促进信息隐藏/封装,从而提高安全性/可靠性。

Oracle 教程 页面很好地介绍了它。

Public int is a variable that has no access control. It is effectively a global variable. Unmodified int is a "protected+" int if you will, it operates as a protected int but can't be used by subclasses. Protected ints can be used by subclasses of the class containing that particular int variable. Private ints can only be used within that class. It's generally advisable to use as few public variables as you can in order to promote information hiding/encapsulation to improve security/reliability.

The Oracle Tutorial page covers it nicely.

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