为什么 oop 语言没有“只读”功能?访问修饰符?

发布于 2024-09-09 19:41:37 字数 497 浏览 3 评论 0原文

每次我编写简单的 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 技术交流群。

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

发布评论

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

评论(9

花想c 2024-09-16 19:41:37

您错误地从您所知道的一种或某些 OOP 语言概括为一般的 OOP 语言。实现只读属性的一些语言示例:

  • C#(感谢 Darin 和 tonio)
  • Delphi(= Object Pascal)
  • Ruby
  • Scala
  • Objective-C(感谢 Rano)
  • ... 更多?

就我个人而言,我很恼火 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:

  • C# (thanks, Darin and tonio)
  • Delphi (= Object Pascal)
  • Ruby
  • Scala
  • Objective-C (thanks, Rano)
  • ... more?

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.

荒路情人 2024-09-16 19:41:37

那么一些OOP语言确实有这样的修饰符。

Well some OOP languages do have such modifier.

浅唱ヾ落雨殇 2024-09-16 19:41:37

在 C# 中,您可以在 set 和 get 上定义具有不同访问限定符的自动属性:

public int Foo { get; private set; }

这样,类实现可以根据其核心内容修改该属性,而客户端代码只能读取它。

In C#, you can define an automatic property with different access qualifiers on the set and get:

public int Foo { get; private set; }

This way, the class implementation can tinker with the property to its heart's content, while client code can only read it.

趴在窗边数星星i 2024-09-16 19:41:37

C# 有 readonly,Java 和其他一些有 final。您可以使用它们将成员变量设置为只读。

在 C# 中,您只需为属性指定一个 getter,这样它就只能读取,而不能更改。

private int _foo;

public int Foo
{
    get { return _foo; }
}

C# has readonly, Java and some others have final. 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.

private int _foo;

public int Foo
{
    get { return _foo; }
}
起风了 2024-09-16 19:41:37

事实上,不,它们并不相同。 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-only foo.x would be read-only for the internal class code as well.

And there are some languages that do have such modifier.

另类 2024-09-16 19:41:37

C# 属性允许轻松定义只读属性。请参阅这篇文章

C# properties allow to define read only properties easily. See this article.

追风人 2024-09-16 19:41:37

在 Delphi 中:

strict private
  FAnswer: integer;
public
  property Answer: integer read FAnswer;

声明一个访问私有字段 FAnswer 的只读属性 Answer。

In Delphi:

strict private
  FAnswer: integer;
public
  property Answer: integer read FAnswer;

Declares a read-only property Answer that accesses private field FAnswer.

野稚 2024-09-16 19:41:37

这个问题主要归结为:为什么不是每种语言都像 C++ 一样具有 const 属性?

这就是它不在 C# 中的原因:

安德斯·海尔斯伯格:是的。关于
const,这很有趣,因为我们
也经常听到这样的抱怨:
“你为什么没有常量?”隐含的
问题是,“你为什么不
具有由强制执行的 const
运行时?”这确实是人们的想法
正在询问,尽管他们没有来
那样说。

const 在 C++ 中起作用的原因是
因为你可以把它扔掉。如果你
无法抛弃,那么你的世界
会很糟糕。如果你声明一个方法
这需要一个 const Bla,你可以通过
它是非常量的 Bla。但如果是
否则你就不能。如果你
声明一个方法,该方法采用
非常量 Bla,你不能通过它
常量 Bla。所以现在你被困住了。所以你
逐渐需要一个cons版本
一切非 const 的东西,还有你
最终进入一个影子世界。在 C++ 中你
摆脱它,因为就像
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#:

Anders Hejlsberg: Yes. With respect to
const, it's interesting, because we
hear that complaint all the time too:
"Why don't you have const?" Implicit
in the question is, "Why don't you
have const that is enforced by the
runtime?" That's really what people
are asking, although they don't come
out and say it that way.

The reason that const works in C++ is
because you can cast it away. If you
couldn't cast it away, then your world
would suck. If you declare a method
that takes a const Bla, you could pass
it a non-const Bla. But if it's the
other way around you can't. If you
declare a method that takes a
non-const Bla, you can't pass it a
const Bla. So now you're stuck. So you
gradually need a const version of
everything that isn't const, and you
end up with a shadow world. In C++ you
get away with it, because as with
anything in C++ it is purely optional
whether you want this check or not.
You can just whack the constness away
if you don't like it.

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.

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