• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

Objective-C 的OOP(下)-类(static)方法、实例方法、overwrite(覆写)、属性(prope ...

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

先来定义一个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] —————-


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
热门推荐
热门话题
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap