Io语言中newSlot和setSlot有什么区别?
在Io语言中,有2种创建槽的方法:newSlot和setSlot。两者似乎都有相似的行为,除了 newSlot 还创建了一个 setter 之外。在什么情况下需要在创建槽的同时创建setter? setter 的具体目的是什么?
In the Io Language, there are 2 methods for creating slots: newSlot and setSlot. Both seem to have similar behavior except newSlot also creates a setter. What cases are there for a needing a setter to be created at the same time as slot creation? What exactly is the purpose of the setter anyway?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我相信它提供了良好的编码实践的便利。因此,如果您想公开对象属性,那么
newSlot
或其同义词::=
是首选方法。newSlot
可以让事情看起来更好。例如。而且它还可以绕过
do()
上下文。例如。Pet 克隆 do (name = petName)
将抛出Exception: Pet does not respond to 'petName'
因为do()
在克隆的Pet
上下文,因此它看不到petName
。因此,您需要使用设置器:
I believe its a convenience which provides good coding practises. Thus if you want to expose an objects attribute then
newSlot
or its synonym::=
are the preferred way to go.newSlot
can make things look nicer. For eg.And also it can get around
do()
context. For eg.The
Pet clone do (name = petName)
will die throwingException: Pet does not respond to 'petName'
becausedo()
is interpreted within the clonedPet
context and so it cannot seepetName
.So instead you need to use the setter: