先來定義一個Human父類
定義部分:
//// Human.h// OOP//// Created by jimmy.yang on 11-2-9.// Copyright 2011 __MyCompanyName__. All rights reserved.//#import <Foundation/Foundation.h>@interface Human : NSObject {BOOL sex;}+(void) toString;-(void) showSex;@end
註:+(void)前的加號,就表示這一個是類方法(static 方法),而-(void)表示這是一個執行個體方法
實現部分:
注意:下面的 -(id) init 即為建構函式。對應的,還有一個-(void)dealloc方法用來釋放資源(類似於解構函式或c#中的dispose()方法)-註:dealloc方法以後在記憶體管理中詳細學習,這裡先不管它。
//// Human.m// OOP//// Created by jimmy.yang on 11-2-9.// Copyright 2011 __MyCompanyName__. All rights reserved.//#import "Human.h"@implementation Human//建構函式-(id) init{NSLog(@"init() in Human is called");sex = TRUE;return(self);}//static類方法+ (void)toString{NSLog(@"this is a class method of Human");}//執行個體方法- (void)showSex{NSLog(@"my sex is %@",sex?@"MALE":@"FEMALE");}@end
再來定義一個Woman子類
定義部分:
//// Woman.h// OOP//// Created by jimmy.yang on 11-2-9.// Copyright 2011 __MyCompanyName__. All rights reserved.//#import <Foundation/Foundation.h>#import "Human.h"@interface Woman : Human {BOOL married;}-(void) canCook:(NSString*) foodName;-(void) setMarried:(BOOL)m;-(BOOL) Married;@end
實現部分:
注意下面的:setMarried 與 Married 就是obj-C中屬性的標準寫法(當然以後還能看到其它簡化的寫法)
//// Woman.m// OOP//// Created by jimmy.yang on 11-2-9.// Copyright 2011 __MyCompanyName__. All rights reserved.//#import "Woman.h"@implementation Woman//Woman類的建構函式-(id) init{NSLog(@"init() in Woman is called!");if (self==[super init]){sex = FALSE;married = FALSE;}return (self);}//overwrite父類中的toString()+(void)toString{NSLog(@"This is Woman's ToString()");}//Woman能做飯-(void)canCook:(NSString*) foodName{NSLog(@"I can cook %@",foodName);}//屬性的setter-(void) setMarried:(BOOL)m{married = m;}//屬性的getter-(BOOL) Married{return married;}@end
main方法中的調用:
#import <Foundation/Foundation.h>#import "Human.h"#import "Woman.h"int main (int argc, const char * argv[]) { NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init]; // insert code here... NSLog(@"Hello, World!");//調用類的“靜態”方法[Human toString];NSLog(@"----------------");//創造一個Human的執行個體Human *man = [Human new];//調用man的showSex方法[man showSex];NSLog(@"----------------");//定義一個Woman子類的執行個體Woman *wife = [Woman new];[wife canCook:@"Rice"];//調用繼承自父類的方法[wife showSex];//設定屬性[wife setMarried:TRUE];//讀取屬性值NSLog(@"wife's married = %@",wife.Married==NO?@"FALSE":@"TRUE");NSLog(@"----------------");//調用overwrite後的toString方法[Woman toString];//Factory模式中常用的手法,在這裡依然適用(只不過編譯時間會有警告 'Human' may not respond to '-canCook:')Human *wife2 = [Woman new];[wife2 canCook:@"soap"];NSLog(@"----------------"); [pool drain]; return 0;}
運行結果:
2011-02-09 17:01:02.016 OOP[1725:a0f] Hello, World!
2011-02-09 17:01:02.053 OOP[1725:a0f] this is a class method of Human
2011-02-09 17:01:02.062 OOP[1725:a0f] ----------------
2011-02-09 17:01:02.075 OOP[1725:a0f] init() in Human is called
2011-02-09 17:01:02.091 OOP[1725:a0f] my sex is MALE
2011-02-09 17:01:02.094 OOP[1725:a0f] ----------------
2011-02-09 17:01:02.099 OOP[1725:a0f] init() in Woman is called!
2011-02-09 17:01:02.104 OOP[1725:a0f] init() in Human is called
2011-02-09 17:01:02.105 OOP[1725:a0f] I can cook Rice
2011-02-09 17:01:02.108 OOP[1725:a0f] my sex is FEMALE
2011-02-09 17:01:02.109 OOP[1725:a0f] wife's married = TRUE
2011-02-09 17:01:02.111 OOP[1725:a0f] ----------------
2011-02-09 17:01:02.116 OOP[1725:a0f] This is Woman's ToString()
2011-02-09 17:01:02.120 OOP[1725:a0f] init() in Woman is called!
2011-02-09 17:01:02.121 OOP[1725:a0f] init() in Human is called
2011-02-09 17:01:02.123 OOP[1725:a0f] I can cook soap
2011-02-09 17:01:02.125 OOP[1725:a0f] ----------------