1。物件需要做2件事才算正式被生出來,先 alloc, 再 init, 如下:
NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
可以用 new 取代
NSAutoreleasePool * pool = [NSAutoreleasePool new] ;
2.籍由在定義class 時指定某些屬性為 property,可節省實作 getter, setter 方法。
@interface Fraction : NSObject
{
int numerator;
int denominator;
}
@property int numerator,denominator;
-(void)print ;
@end
在實作class也必須對應下一個指令,告訴compiler自動產生對應的getter,setter。
@implementation Fraction
@synthesize numerator,denominator;
-(void)print {
NSLog(@"%i/%i", numerator,denominator);
}
@end
3.Property 可以直接用 . assign value,取代 setter
myFrac1.numerator=5 ;
跟
[myFrac1 setNumerator:5] ; 是相同的。
4.接受多個參數的方法
-(void) setTo: (int)n over: (int) d ;
使用時
[aFraction setTo:5 over:10] ;
多個參數時,參數名稱也算是方法名稱。
沒有留言:
張貼留言