先来定义一个Human父类
定义部分:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
|
// // 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 即为构造函数.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36
|
// // 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子类
定义部分:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
|
// // 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中属性的标准写法(当然以后还能看到其它简化的写法)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48
|
// // 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方法中的调用:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53
|
#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] —————-