我在练习Io语言。寻找资源非常困难。如你所知,Io语言没有任何课程。但是我们可以根据我们的需要创建我们的课程。无论如何,假设我希望能够运行它并创建一个Animal类。创建一个构造函数,在Io语言中添加属性和方法
Animal := Class new("Animal",
withConstructor(nameParam,
name = nameParam
)
withInstanceAttribute("name", "")
withInstanceAttribute("foodItemsEaten", 0)
withStaticAttribute("allFoodItemsEaten", 0)
withMethod("feed", howMuchFood,
foodItemsEaten = foodItemsEaten + howMuchFood
class allFoodItemsEaten = allFoodItemsEaten + howMuchFood
"#{name} has just eaten #{howMuchFood} food items." interpolate println
)
withMethod("feedSummary",
"So far #{foodItemsEaten} food items eaten by #{name} and #{class allFoodItemsEaten} in total by all dogs." interpolate println
)
)
所以我有这个类的结构:
Class := Object clone
Class new := method(name, // Meta-class constructor
cls := Class clone
cls __name := name // Name of the class
cls __instanceProto := Object clone // Prototype of instances - hold instance field along with their initial values
cls __instanceProto class := cls
call argAt(1) doInContext(cls)
cls
)
我想补充withConstructor方法的例子,但我甚至无法读取参数。
Class withConstructor := method(nameParam
self
)
我只是无法处理它,甚至构造函数。它说“类不响应nameParam”。语法很简单,但我想我仍然没有弄清楚语言的结构。任何想法,类似的资源,或能够解释它的人?
我知道这个例子很好理解语言结构。我会做更多的读物。我非常喜欢IO,但不幸的是,没有那么多资源。 –
好吧,我创建了withConstructor部分。我如何添加实例属性?有小费吗? –
就是这样,“实例”和“类/静态”属性之间没有区别。唯一的区别是你如何使用它们。您可以直接将'allFoodItemsEaten'添加到'Animal',如果您想设置或更新,请告诉Animal更新它,而不是自我。 – jer