Io语言updateSlot、setSlot和newSlot的区别

来源:互联网 发布:365源码网 编辑:程序博客网 时间:2024/06/05 07:41
newSlot(slotName, aValue)

Creates a getter and setter for the slot with the name slotName and sets its default value to aValue. Returns self. For example, newSlot("foo", 1) would create slot named foo with the value 1 as well as a setter method setFoo().
::= 对应为 newSlot
setSlot(slotNameString, valueObject)

Sets the slot slotNameString in the receiver to hold valueObject. Returns valueObject.
:= 对应为 setSlot
updateSlot(slotNameString, valueObject)

Same as setSlot(), but raises an error if the slot does not already exist in the receiver's slot lookup path.
= 对应为 updateSlot
以上是官方的文档中的区别。
updateSlot的功能是比较好理解的。
主要问题就是setSlot和newSlot的区别
例子:
Account := Object clone
Account balance := 0
Account deposit := method(v, balance := balance + v)
myAccount := Account clone
myAccount deposit(10)
myAccount balance println
myAccount slotNames println
Account balance println
Account slotNames println
输出:

0
list()
0
list(balance, deposit, type)
Account中的deposit调用的方法并没有改变balance的值
如果把:= 换成 ::= 则myAccount 中有slot:balance,Account中也有slot:balance,deposit 方法为myAccount创建了balance slot并赋值为Account balance加10。
如果把:=换成=,则myAccount中没有slot:balance,但是其原型Account有slot:balance。
原创粉丝点击