2003/05/25
前回のつづきです。
- (BOOL) isEqual: (id)another
{
return TRUE;
}
|
[self assert:zero notEqual:[SmallInteger initWithInt:1]]; |
[self assertFalse:[zero isEqual:[SmallInteger initWithInt:1]]]; |
@interface SmallInteger : NSObject {
int value;
}
|
+ (id) initWithInt: (int)initValue
{
SmallInteger *newValue = [[[SmallInteger alloc] initWithInt:initValue] autorelease];
return newValue;
}
- (id) initWithInt: (int)initValue
{
value = initValue;
return self;
}
|
- (BOOL) isEqual: (id)another
{
if (value == [another intValue]) {
return TRUE;
} else {
return FALSE;
}
}
|
- (int) intValue
{
return value;
}
|
次は、加算してみましょう。
- (void) testAdd
{
SmallInteger* one = [SmallInteger initWithInt:1];
SmallInteger* two = [one add:[SmallInteger initWithInt:1]];
[self assert:two equals:[SmallInteger initWithInt:2]];
}
|
まずは、コンパイルエラーです。つづきは、また今度。
メソッドisEqual:の引数の型は、id型です。このメソッドの中で、メソッドintValueを呼び出すときに、idというクラスがintValueというメソッドを持っているわけじゃありません。まして、自分のメソッドとして呼び出しているわけでもありません。
C++しか知らないと、こういうところが理解しにくいかもしれませんね。