0

我创建了一个名为 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 |
    ]
]
4

2 回答 2

0

您缺少一个时期:

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

您之前的代码:

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

可以改写为:

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

因此错误指出符号#lynx 不理解#lynx 消息

于 2014-05-06T16:33:18.153 回答
0

扩展 Uko 的评论:

编译器可能无法分辨这animal是一个类,因为名称没有大写。相反,编译器认为它是一个变量名。该变量看起来未初始化,因此是nil. 这就是为什么您收到MessageNotUnderstood消息 send 异常的原因#lynx

于 2014-03-27T05:00:10.257 回答