Smalltalk用户定义的子类问题。

时间:2014-03-26 22:35:31

标签: object subclass smalltalk subclassing gnu-smalltalk

我创建了一个名为animal的类,我想创建该类的两个子类,我刚刚创建了lynx和rabbit。然而,当我尝试编译程序时,我在我定义我的第一个动物子类lynx的行上得到以下错误:

Object: #lynx error: did not understand #lynx
MessageNotUnderstood(Exception)>>signal (ExcHandling.st:254)
Symbol(Object)>>doesNotUnderstand: #lynx (SysExcept.st:1407)
UndefinedObject>>executeStatements (newanimal.st:121)
Object: nil error: did not understand #comment:
MessageNotUnderstood(Exception)>>signal (ExcHandling.st:254)
UndefinedObject(Object)>>doesNotUnderstand: #comment: (SysExcept.st:1407)
UndefinedObject>>executeStatements (newanimal.st:123)
newanimal.st:125: key lynx not found
newanimal.st:125: expected Eval, Namespace or class     definition
newanimal.st:134: expected Eval, Namespace or class definition
newanimal.st:137: expected expression

我在定义了动物(它是对象的子类)后立即定义了lynx子类。这是我的两个类的代码。

Object subclass: #animal .
animal instanceVariableNames: ' animals '.
animal class instanceVariableNames: ' id type '.
animal comment: 'I am the class for all animals' .

animal class extend [
    create: type [
        (animals := nil) ifTrue: [ self init ].
        (type == 'rabbit') ifTrue: [animals := rabbit new] .
        (type == 'lynx') ifTrue: [animals := lynx new] .
        ^ animals .
    ]
    getid ["returns the animals unique id"
        | tempAnimal temp |
        tempAnimal := grid asArray at: 'id' .
        "temp :=  ."
    ]
    getrow: id ["returns the animals grid row"
        | tempAnimal temp |
        grid do: [:each | 
            tempAnimal := grid at: each .
            (tempAnimal at: id == id) ifTrue: [temp:= tempAnimal at: 'row'. ^ temp ] . ]

    ]
    getcol: id ["returns the animals grid col"
        | tempAnimal temp |
        grid do: [:each | 
            tempAnimal := grid at: each .
            (tempAnimal at: id == id) ifTrue: [temp:= tempAnimal at: 'col'. ^ temp ] . ]

    ]
    getdirection: id ["returns the animals movement direction"
        | tempAnimal temp |
        grid do: [:each | 
            tempAnimal := grid at: each .
            (tempAnimal at: id == id) ifTrue: [temp:= tempAnimal at: 'direction'. ^ temp ] . ]
    ]
    setdirection ["sets animals movement direction"
        | direction |
        direction := grid rand .
        ^ direction .
    ]
]
animal extend [
    init [
        animals := Dictionary new.
    ]
]

#animal subclass: #lynx
lynx instanceVariableNames: ' direction '.
lynx class instanceVariableNames: ' lynxdictionary '. 
lynx comment: 'I am the subclass of animal that is lynxs' .

lynx class extend [
    new [
        lynxdictionary := Dictionary new .
        lynxdictionary add: 'type' -> 'lynx' .
        direction := animal setdirection .
        lynxdictionary add: 'direction' -> direction .
        lynxdictionary := grid placerow:lynxdictionary .
        ^ lynxdictionary .
    ]
    act [
        | row col tempAniaml |
    ]
]

2 个答案:

答案 0 :(得分:0)

扩展Uko的评论:

编译器可能无法告诉animal是一个类,因为名称不是大写的。相反,编译器认为它是变量名。该变量看起来未初始化,因此nil。这就是为什么您收到发送MessageNotUnderstood邮件的#lynx例外。

答案 1 :(得分:0)

你错过了一段时间:

#animal subclass: #lynx. "<---- added period here"
lynx instanceVariableNames: ' direction '.

您之前的代码:

#animal subclass: #lynx
lynx instanceVariableNames: ' direction '.

可以改写为:

#animal subclass: #lynx lynx instanceVariableNames: ' direction '.

因此错误表明符号#lynx不理解#lynx消息