Smalltalk 公共初始化
有没有办法“隐藏”初始化方法,以便在构造后无法调用它?
我想要这样的东西:
Class>>new: params
^super newInstance initializedBy: [
"actual initialization code"
]
Is there any way to "hide" initialize method so it couldn't be called after construction?
I would like something like:
Class>>new: params
^super newInstance initializedBy: [
"actual initialization code"
]
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
您可以这样做:
但是,您仍然有一个初始化方法,但现在它位于类一侧,并且并没有真正执行您想要的操作。然而..你可以尝试:
在实例方面:
我认为这可以概括你想要的东西。我认为这不值得付出努力,因为我可以使用 instVarNamed 来更改您的对象。 Smalltalk 约定很简单...外部人员不应该调用您的初始化方法类别中的任何内容,而不能很好地理解他们这样做的原因。
您也可以将其全部保留在班级一侧:
You could do something like this:
but, you still have an initialize method of sorts but now it is on the class side and doesn't really do what you want. however.. you could try:
and on the instance side:
I think that gets at the general idea of what you wanted. I don't think its worth the effort as I can just reach in using instVarNamed and change your object around. The smalltalk convention is simple... no one from the outside should call anything in your initialization method category w/o a really good understanding of why they are doing it.
You could also keep it all on your class side w:
听起来像是 Seuss 的工作,我的依赖注入框架,它为您提供了对初始化过程的大量控制(并且默认情况下,在将构造参数传递到设置器之后调用初始化)。不过,它还没有发布。
要回答你的问题,不使用 Seuss,你可以覆盖 new,这样它就不会调用
initialize:
Sounds like a job for Seuss, my dependency injection framework, which gives you a great deal of control over the initialization process (and, by default, calls initialize AFTER construction parameters are passed into setters). It's still unreleased, though.
To answer your question, without using Seuss, you can overwrite new, so that it does not call
initialize:
IIUC 你问是否可以将一个方法设为私有。答案是肯定和否定:) Smalltalk 中的约定是将“私有”方法放在“私有”类别中。这是向外部用户发出的一个信号,表明它不能被使用,但无论如何都无法阻止他们实际使用它。在实践中,这似乎工作得很好。
这就是你问的吗?如果没有,更多细节会有所帮助。
IIUC you're asking if you can make a method private. The answer is yes and no :) The convention in Smalltalk is to put "private" methods in the "private" category. This is a signal to outside users that it's not to be used, but nothing prevents them from actually using it anyway. In practice this seems to work fine.
Is that what you were asking? If not, some more details would help.
你也可以不实现#initialize,无论你想放在那里但不是从#new调用,你都可以将它放在另一个具有另一个名称的方法中。
You can also NOT implement #initialize and whatever you wanted to put there but not called from #new, you put it in another method with another name.
我可以想到实现最终目标的几种不同方法,
a)添加“初始化”实例变量并在设置为 true 时跳过#initialize,
b)添加一个测试用例,通过您的代码运行重写搜索并计算发件人数量的 #initialize 不在其自己的类层次结构的 #new 方法中。事实上,如果您自己的类继承自通用模型,那么您实际上应该只有一个#initialize 发送者,并且可以在测试用例中轻松断言这一点。
希望这有帮助。
I can think of a couple of different ways of achieving the end goal,
a) add 'initialized' instance variable and skip #initialize when set to true,
b) add a test case that runs a rewrite search through your code and counts the senders of #initialize that aren't in the #new method(s) of its own class hierarchy. In fact, if your own classes inherit from a common model, you should really only have one sender of #initialize, and that can be easily asserted in a test case.
Hope this helps.
实例方法:
类方法:
在我的(ST/X)图像中尝试过 - 有效!
当然,辅助方法可以放置在层次结构中更高的位置......
instance methods:
class methods:
tried it in my (ST/X) image - works!
Of course, the helper methods could be placed higher up in the hierarchy...