저는 스몰 토크 (VisualAge 환경)에서 새로 왔으며 그녀의 인스턴스 개수를 세는 클래스를 만들려고합니다. 불행히도 '새로운'방법을 무시할 때 무언가 작동하지 않습니다.스몰 토크에서 클래스의 인스턴스를 만드는 방법은 무엇입니까?
Object subclass: #TestClassB
instanceVariableNames: 'niceVariable '
classVariableNames: 'InstanceCounter '
poolDictionaries: ''!
!TestClassB class publicMethods !
initWithNiceParameter: parameter
|testClassBInstance|
testClassBInstance:= self new.
^(testClassBInstance niceVariable: parameter)!
new
super new.
InstanceCounter isNil
ifTrue: [InstanceCounter := 0]
ifFalse: [InstanceCounter := InstanceCounter + 1].
^self
! !
!TestClassB publicMethods !
niceVariable: anObject
"Save the value of niceVariable."
niceVariable := anObject.
! !
내가 'initWithNiceParameter'메시지와 함께 새로운 객체 생성하고 싶습니다 : 이것은 내 수업 코드
TestClassB initWithNiceParameter: 'my super string'
을하지만 얻을 수있는 모든 오류입니다 :
TestClassB does not understand niceVariable:
그것은 때문이다 'TestClassB'도 개체이며 'niceVariable'설정자가없는 것으로 보입니다.
'새'방법을 무시할 때 개체를 만드는 방법을 알고 있습니까?
우리가 개선하는 동안, 클래스 측 # 초기화에서 InstanceCounter를 0으로 초기화하고 분기를 완전히 제거합시다. –
'TestClassB allInstances size'가 충분하지 않습니까? –
@SeanDeNigris # 초기화 및 분기 제거로 무엇을 의미합니까? 나는이 메소드를 만들었다. (#new와 같은 클래스 측 메소드이다.) Object 클래스에서 #initialize를 오버라이드하지만 새로운 객체를 생성하면 자동으로 호출되지 않는다. – user3452568