我正在练习Io语言。找货源太难了。如您所知,Io 语言没有类。但是我们可以根据需要创建我们的类。无论如何,假设我希望能够运行它并创建一个 Animal 类。
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”。语法很简单,但我想我仍然没有弄清楚语言的结构。任何想法,类似来源或可以解释的人?