首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Smalltalk用户定义的子类问题。

Smalltalk用户定义的子类问题。
EN

Stack Overflow用户
提问于 2014-03-27 06:35:31
回答 2查看 1.5K关注 0票数 0

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

代码语言:javascript
复制
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的一个子类。下面是这两个类的代码。

代码语言:javascript
复制
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 |
    ]
]
EN

回答 2

Stack Overflow用户

发布于 2014-03-27 13:00:10

对Uko的评论进行扩展:

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

票数 0
EN

Stack Overflow用户

发布于 2014-05-07 00:33:18

您缺少一个句点:

代码语言:javascript
复制
#animal subclass: #lynx. "<---- added period here"
lynx instanceVariableNames: ' direction '.

您之前的代码:

代码语言:javascript
复制
#animal subclass: #lynx
lynx instanceVariableNames: ' direction '.

可以重写为:

代码语言:javascript
复制
#animal subclass: #lynx lynx instanceVariableNames: ' direction '.

因此出现错误,说明符号#lynx不理解#lynx消息

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/22674487

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档