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 сразу после того, как определил animal, который является подклассом объекта. Вот мой код для обоих классов.
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 ответа
Вы пропустили точку:
#animal subclass: #lynx. "<---- added period here"
lynx instanceVariableNames: ' direction '.
Ваш предыдущий код:
#animal subclass: #lynx
lynx instanceVariableNames: ' direction '.
может быть переписан как:
#animal subclass: #lynx lynx instanceVariableNames: ' direction '.
Отсюда ошибка, указывающая, что Символ #lynx не понял сообщение #lynx
Расширяя комментарий Уко:
Компилятор, вероятно, не может сказать, что animal
это класс, потому что имя не с большой буквы. Вместо этого компилятор считает, что это имя переменной. Эта переменная выглядит неинициализированной и поэтому nil
, Вот почему вы получаете MessageNotUnderstood
исключение для отправки сообщения #lynx
,