C# 与 Java 中的访问修饰符最佳实践
我知道 OOD 的经验法则是在合理范围内尽可能减少对给定对象所有成员的访问。
C# 和 Java 似乎都实现了同一组访问修饰符; 然而,让我困惑了一段时间的是,为什么 Java 类似乎大多被声明为公共类,而 C# 类似乎大多被声明为默认类。 这些语言是否存在一些微妙之处,强制要求这些类差异,或者只是一个约定问题还是什么?
我发现自己经常检查我的 C# 代码(我习惯性地将大多数类公开,除了内部类、匿名类和其他范围狭窄且有用的类)试图取悦编译器,但我想知道我是否可能遗漏了一些重要的东西。
I understand that the rule of thumb in OOD is to minimize access to all members of a given object as best as can be reasonably accomplished.
C# and Java both seem to implement the same set of access modifiers; however, something which has bewildered me for some time now is why Java classes seem to be mostly declared as public while C# classes seem mostly to be declared as default. Is there some subtlety to these languages which imposes these differences, or is it simply a matter of convention or what?
I find myself frequently going through my C# code (I habitually make most classes public, excepting inner classes, anonymous classes, and other classes of narrow scope and usefulness) in an attempt to please the compiler, however I wonder if I may be missing something important.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
我想你回答了你的问题。 根据 Joshua Bloch 的说法,“经验法则很简单,让每个班级或成员尽可能难以接近。” 高效 Java
I think you answered your question. As per Joshua Bloch, "The rule of thumb is simple, make each class or member as inaccessible as possible." Effective Java
Java 的作用域 与 C# 的范围。
从 Java 开发人员的角度看 C# 的相同但不同之处对此进行了简要讨论:< a href="http://www.25hoursaday.com/CsharpVsJava.html#access" rel="noreferrer">访问修饰符。 该文档现在有些过时,但仍然具有重要意义。
该列表有两个错误:
internal
相当于 Java 的默认作用域(即它自己的作用域)。内部 protected
相当于 Java 的protected
。此外,上述文档没有提及类的默认访问修饰符是什么,仅提及方法和属性/变量。
作为参考,c# 中类的默认作用域是内部的。 如前所述,Java 是其通常的默认范围。
Java's scoping is slightly different than C#'s scoping.
This is talked about briefly in C# From a Java Developer's Perspective's The Same, But Different: Access Modifiers. This document is slightly dated now, but is still mostly relevant.
That list has two mistakes:
internal
is equivalent to Java's default scope (which is its own scope).internal protected
is equivalent to Java'sprotected
.Additionally, the above document doesn't mention what the default access modifiers are for classes, only for methods and properties/variables.
For reference, the default scope for classes in c# is internal. Java's is its usual default scope, as described earlier.
我唯一公开的是静态/最终变量,它们通常是常量。 其他一切都是私有的,并且在适当的时候通过 getXXX() 和 setXXX() 方法完成访问。 setXXX() 方法还对数据执行任何验证。 如果我必须保护某些东西,我会的,但我通常会避免这样做。
The only things that I make public are static/final variables, which are generally constants. Everything else is private, and access is done through getXXX() and setXXX() methods, when appropriate. The setXXX() methods also perform any validation against the data. If I have to make something protected, I will, but I usually avoid it.
更少的“客户”(其他代码)了解类的内部工作原理,他会受益更多......简单的抽象规则,以及 OOP 的基本支柱。 上面已经给出了正确的答案:
“经验法则很简单,让每个类或成员尽可能难以访问。” 〜约书亚·布洛赫
Less "client" (other code) knows about inner-workings of your classes, he will benefit more ... Simple rule of abstraction, and a fundamental pillar of OOP. The right answer is already given above:
"The rule of thumb is simple, make each class or member as inaccessible as possible." ~ Joshua Bloch