Smalltalk 公共方法与私有/受保护方法

发布于 2024-12-04 10:24:27 字数 1431 浏览 2 评论 0原文

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

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

发布评论

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

评论(4

捂风挽笑 2024-12-11 10:24:27

事实上,Smalltalk 的方式是将私有方法放在“私有”类别中。这表明您不应该使用这些方法,但当然并不强制这样做。

这是设计使然 - 这是一个功能,而不是一个错误。 Smalltalk 从一开始就被设计为一个开放系统。

一些优点:

  • 如果我只是必须——也许库设计者没有预见到需要公开一些我必须拥有的特定东西——我仍然可以调用这些私有方法。显然,这不是一个可以轻易做的事情:而是明智、谨慎地做,因为知道这是一种战术解决方案。
  • 语言简洁。
  • (根据 Alexandre Jasmin 的评论)Smalltalk 不区分程序员可以做什么和语言/环境可以做什么。这意味着 Smalltalk-the-image 公开了您构建自己的检查器/调试器/其他内容所需的所有内容,而无需使用我们可以做到这一点但您不能的技术提供特殊工具。

Indeed, the Smalltalk way is to put private methods in the 'private' category. This indicates that you shouldn't use these methods, but of course doesn't enforce this.

This is by design - it's a feature, not a bug. Smalltalk was designed from the beginning precisely to be an open system.

Some advantages:

  • If I simply have to - maybe the library designer didn't foresee a need to expose some particular thing I simply have to have - I can still call those private methods. Obviously, this isn't something one does lightly: rather, judiciously, cautiously, knowing that it's a tactical solution.
  • Language simplicity.
  • (As per Alexandre Jasmin's comment) Smalltalk makes no distinction between what you, the programmer, can do and what the language/environment can do. That means that Smalltalk-the-image exposes all the things needed for you to build your own inspectors/debuggers/whatever without having to supply special tools using we-can-do-this-but-you-can't techniques.
骄兵必败 2024-12-11 10:24:27

事实上,私有方法和受保护方法是 c++、java 和 c# 等语言的一个重大弱点。他们基本上对用户说:我不想学习和发展。其结果(以及更多的早期绑定)是这些语言需要更多的 BDUF,因此对于现代(敏捷)开发过程来说不太适用。

Private and protected methods are in fact a significant weakness of languages like c++, java and c#. They basically say to their users: I don't want to learn and evolve. The consequence of that (and a lot more early binding) is that those languages require much more BDUF and are thus far less usable for a modern (agile) development process.

一紙繁鸢 2024-12-11 10:24:27

第一个问题是私有/受保护的访问修饰符是什么?从根本上来说,这与安全无关。它是关于向用户公开正确的界面。从这里开始,保护/私有类别和专门为此构建的语言结构之间没有什么区别。

我什至会说,拥有私有/受保护的可见性修饰符会给问题带来比它实际解决的更复杂的问题。

除此之外,我不认为私有/受保护的可见性是解决这个问题

The first question is what private/protected access modifiers are about? Fundamentally, it is not about safety or security. It is about exposing the right interface to the user. Starting from that, it makes little difference between having categories protected/private and a language construct specifically for that.

I would even say that having private/protected visibility modifier brings more complexity to the problem than it actually solves.

Besides that, I don't think that private/protected visibility is a good answer to this problem

撩动你心 2024-12-11 10:24:27

至少,Smalltalk 应该有这样的文本约定:以“下划线”开头的方法名称禁止在对象本身之外调用。不幸的是,我认为不允许将“下划线”作为方法名称的第一个字符。

At the least, Smalltalk should have the textual convention that method names that begin with 'underscore' are verboten to call outside of the objects themselves. Unfortunately, I don't think that 'underscore' is allowed as the first character of a method name.

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