为什么 oop 语言没有“只读”功能?访问修饰符?
每次我编写简单的 getter(仅返回成员值的获取函数)时,我想知道为什么 oop 语言不简单地有一个“只读”访问修饰符,该修饰符允许读取对象成员的值但不允许你像 c++ 中的 const 一样设置它们。
private、protected、public 访问修饰符为您提供完全(读/写)访问权限或无访问权限。
编写 getter 并每次调用它都很慢,因为函数调用比仅访问成员慢。一个好的优化器可以优化这些 getter 调用,但这就是“魔法”。我认为学习某个编译器的优化器如何工作并编写代码来利用它并不是一个好主意。
那么,为什么我们需要在实践中到处编写访问器、只读接口,而只需一个新的访问修饰符就可以解决问题呢?
ps1:请不要说“这会破坏封装”之类的话。公共 foo.getX()
和公共但只读的 foo.x
会做同样的事情。
编辑:我的帖子没有写清楚。对不起。我的意思是你可以在外面读取成员的值,但不能设置它。您只能在类范围内设置其值。
Every time I write trivial getters (get functions that just return the value of the member) I wonder why don't oop languages simply have a 'read only' access modifier that would allow reading the value of the members of the object but does not allow you to set them just like const things in c++.
The private,protected,public access modifiers gives you either full (read/write) access or no access.
Writing a getter and calling it every time is slow, because function calling is slower than just accessing a member. A good optimizer can optimize these getter calls out but this is 'magic'. And I don't think it is good idea learning how an optimizer of a certain compiler works and write code to exploit it.
So why do we need to write accessors, read only interfaces everywhere in practice when just a new access modifier would do the trick?
ps1: please don't tell things like 'It would break the encapsulation'. A public foo.getX()
and a public but read only foo.x
would do the same thing.
EDIT: I didn't composed my post clear. Sorry. I mean you can read the member's value outside but you can't set it. You can only set its value inside the class scope.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(9)
您错误地从您所知道的一种或某些 OOP 语言概括为一般的 OOP 语言。实现只读属性的一些语言示例:
就我个人而言,我很恼火 Java 没有这个功能(还没有?)。在其他语言中看到过这个特性后,用 Java 编写样板代码就显得很无聊。
You're incorrectly generalizing from one or some OOP language(s) you know to OOP languages in general. Some examples of languages that implement read-only attributes:
Personally, I'm annoyed that Java doesn't have this (yet?). Having seen the feature in other languages makes boilerplate writing in Java seem tiresome.
那么一些OOP语言确实有这样的修饰符。
Well some OOP languages do have such modifier.
在 C# 中,您可以在 set 和 get 上定义具有不同访问限定符的自动属性:
这样,类实现可以根据其核心内容修改该属性,而客户端代码只能读取它。
In C#, you can define an automatic property with different access qualifiers on the set and get:
This way, the class implementation can tinker with the property to its heart's content, while client code can only read it.
C# 有
readonly
,Java 和其他一些有final
。您可以使用它们将成员变量设置为只读。在 C# 中,您只需为属性指定一个 getter,这样它就只能读取,而不能更改。
C# has
readonly
, Java and some others havefinal
. You can use these to make your member variables read-only.In C#, you can just specify a getter for your property so it can only be read, not changed.
事实上,不,它们并不相同。 Public foo.getX() 仍然允许内部类代码写入变量。只读的
foo.x
对于内部类代码也是只读的。有些语言确实有这样的修饰符。
Actually, no they aren't the same. Public
foo.getX()
would still allow the internal class code to write to the variable. A read-onlyfoo.x
would be read-only for the internal class code as well.And there are some languages that do have such modifier.
C# 属性允许轻松定义只读属性。请参阅这篇文章。
C# properties allow to define read only properties easily. See this article.
更不用说 Objective-C 2.0 属性只读访问器了
http://developer.apple.com/ mac/library/documentation/Cocoa/Conceptual/ObjectiveC/Articles/ocProperties.html
Not to mention Objective-C 2.0 property read-only accessors
http://developer.apple.com/mac/library/documentation/Cocoa/Conceptual/ObjectiveC/Articles/ocProperties.html
在 Delphi 中:
声明一个访问私有字段 FAnswer 的只读属性 Answer。
In Delphi:
Declares a read-only property Answer that accesses private field FAnswer.
这个问题主要归结为:为什么不是每种语言都像 C++ 一样具有 const 属性?
这就是它不在 C# 中的原因:
请参阅:http://www.artima.com/intv/choicesP.html
, wy const 在 C++ 中工作的原因是因为你可以解决它。这对于起源于 C 的 C++ 来说是明智的。
对于 Java 和 C# 等托管语言,用户会期望 const 与垃圾收集器一样安全。这也意味着你无法解决它,如果你无法解决它,它就没有用处。
The question largely boils down to: why does not every language have a const property like C++?
This is why it's not in C#:
See: http://www.artima.com/intv/choicesP.html
So, the reason wy const works in C++ is because you can work around it. Which is sensible for C++, which has its roots in C.
For managed languages like Java and C#, users would expect that const would be just as secure as, say, the garbage collector. That also implies you can't work around it, and it won't be useful if you can't work around it.