XCode 开发时,一个小BUG,记录之.
1):Rectangle.h
//
// Created by dhanzhang on 10-6-11.
// Copyright 2010 __MyCompanyName__. All rights reserved.
//
#import <Cocoa/Cocoa.h>
@interface Rectangle : NSObject {
int width;
int height;
}
-(Rectangle*) initWithWidth: (int) w height: (int) h;
-(void) setWidth: (int) w;
-(void) setHeight: (int) h;
-(void) setWidth: (int) w height: (int) h;
-(int) width;
-(int) height;
-(void) print;
@end
@interface Square: Rectangle
-(Square*) initWithSize: (int) s;
-(void) setSize: (int) s;
-(int) size;
@end
@implementation Rectangle
-(Rectangle*) initWithWidth: (int) w height: (int) h {
self = [super init];
if ( self ) {
[self setWidth: w height: h];
}
return self;
}
-(void) setWidth: (int) w {
width = w;
}
-(void) setHeight: (int) h {
height = h;
}
-(void) setWidth: (int) w height: (int) h {
width = w;
height = h;
}
-(int) width {
return width;
}
-(int) height {
return height;
}
-(void) print {
printf( "width = %i, height = %i", width, height );
}
@end
//-----------Square
@implementation Square
-(Square*) initWithSize: (int) s {
self = [super init];
if ( self ) {
[self setSize: s];
}
return self;
}
-(void) setSize: (int) s {
width = s;
height = s;
}
-(int) size {
return width;
}
-(void) setWidth: (int) w {
[self setSize: w];
}
-(void) setHeight: (int) h {
[self setSize: h];
}
@end
------------------调用时的一个小问题---------
void RECT()
{
Rectangle *rec = [[Rectangle alloc] initWithWidth: 10 height: 20];
Square *sq = [[Square alloc] initWithSize:13 ];//------------Build 失败.....
//正确的写法如下:
/*-------不知道会不会造成泄露
Square *sq = [[Square alloc] init ];[sq initWithSize:13];//----调用了2次?
*/
//或者如下
Square *sq = [(Square *)[Square alloc] initWithSize:13 ];
[sq setWidth: 20];
[sq print];
[rec release];
[sq release];
}
浙公网安备 33010602011771号