2014-03-26 3 views
0

나는 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 | 
    ] 
] 
+1

대문자로 쓰여진 클래스 이름을 써주세요. 이것은 관례이며 코드를 읽기 쉽게 만듭니다. – Uko

답변

0

는 :

컴파일러는 아마 이름이 대문자로하지 않기 때문에 animal 클래스라고 말할 수 없다. 대신 컴파일러는 변수 이름이라고 생각합니다. 이 변수는 초기화되지 않은 것으로 보이므로 nil입니다. 그렇기 때문에 메시지 수신에 대한 예외는 #lynx입니다.

0

당신은 기간을 누락 :

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

이전 코드 :

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

는 다시 작성할 수로 :

따라서
#animal subclass: #lynx lynx instanceVariableNames: ' direction '. 

기호 #lynx했던 없다는 오류 #lynx 메시지를 이해하지 못함

관련 문제