나는 animal이라는 클래스를 만들었고 나는 방금 lynx와 rabbit를 만든 클래스의 하위 클래스 두 개를 만들고 싶습니다. 그러나 때 내가 동물의 첫 서브 클래스를 정의하는 줄에 다음과 같은 오류가 프로그램, 살쾡이 컴파일하려고 지금까지 : Smalltalk 사용자 정의 하위 클래스 문제.
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
내가 서브 클래스가 나는 동물을 정의한 후 바로 스라소니 서브 클래스를 정의를 개체의. 다음은 두 클래스의 코드입니다. UKO의 의견에 확장
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 |
]
]
대문자로 쓰여진 클래스 이름을 써주세요. 이것은 관례이며 코드를 읽기 쉽게 만듭니다. – Uko