// Copyright 2011 __MyCompanyName__. All rights reserved.
//
#import "Human.h"
@implementationHuman
//构造函数
-(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"
@interfaceWoman : Human {
BOOLmarried;
}
-(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"
@implementationWoman
//Woman类的构造函数
-(id) init{
NSLog(@"init() in Woman is called!");
if(self==[superinit]){
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
{
returnmarried;
}
@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"
intmain (intargc, constchar* argv[]) {
NSAutoreleasePool* pool = [[NSAutoreleasePoolalloc] 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];
return0;
}
运行结果:
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] ----------------
请发表评论