Objective C 2.0 简明教程 (5) 属性(Property)
来源:互联网 发布:智库网站 知乎 编辑:程序博客网 时间:2024/06/15 20:32
Objective C 2.0 简明教程 (5) 属性(Property)
作者:Administrator 周六, 2009年 03月 28日 07:47Objective C 2.0 为我们提供了property。它大大简化了我们创建数据成员读写函数的过程,更为关键的是它提供了一种更为简洁,易于理解的方式来访问数据成员。
我们先来看一下在Objective C 1.x下我们声明Book类的头文件:1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
//
// Book.h
#import <Cocoa/Cocoa.h>
@interface Book : NSObject {
NSString *title;
NSNumber* numofpages;
}
- (id)initWithTitle:(NSString*) booktitle andNumofpages:(NSNumber*) num;
- (NSString*) title;
- (void) setTitle:(NSString*)newtitle;
- (NSNumber*) numofpages;
- (void) setNumofpages:(NSNumber*)newnumofpages;
- (NSString*) summary;
@end
在Objective C 2.0下,我们可以通过声明与数据成员同名的property来省去读写函数的声明。代码如下所示:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
//
// Book.h
#import <Cocoa/Cocoa.h>
@interface Book : NSObject {
NSString *title;
NSNumber* numofpages;
}
- (id)initWithTitle:(NSString*) booktitle andNumofpages:(NSNumber*) num;
@property (retain) NSString* title;
@property (retain) NSNumber* numofpages;
@property (readonly) NSString* summary;
@end
我们为每一个数据成员声明了一个property。即使Book类中没有summary这个数据成员,我们同样可以声明一个名为summary的property。声明property的语法为:
@property (参数) 类型 名字;
这里的参数主要分为三类:读写属性(readwrite/readonly),setter语意(assign/retain/copy)以及atomicity(nonatomic)。
assign/retain/copy决定了以何种方式对数据成员赋予新值。我们在声明summary propery时使用了readonly,说明客户端只能对该property进行读取。atomicity的默认值是atomic,读取函数为原子操作。
下面我们来看一下在Objective C 1.x 下implementation文件:
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
54
55
56
57
//
// Book.m
#import "Book.h"
@implementation Book
//@synthesize title;
- (id)initWithTitle:(NSString*) booktitle andNumofpages:(NSNumber*) num{
self = [super init];
if(nil != self)
{
[self setNumofpages:num];
[self setTitle:booktitle];
}
return self;
}
- (NSString*) title{
return title;
}
- (void) setTitle:(NSString*)newtitle{
[title release];
title = [newtitle retain];
}
- (NSString*) description{
return title;
}
- (NSNumber*) numofpages{
return numofpages;
}
- (void) setNumofpages:(NSNumber*)newnumofpages{
[numofpages release];
numofpages = [newnumofpages retain];
}
-(NSString*) summary
{
NSString* retstr = [[NSString alloc]initWithFormat:@"Title: %@, Number of pages: %@",
title, numofpages];
[retstr autorelease];
return retstr;
}
- (void) dealloc{
[numofpages release];
[title release];
[super dealloc];
}
@end
在objective c 2.0下,由于我们声明了property,implementation文件可以更改如下:
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
//
// Book.m
#import "Book.h"
@implementation Book
@synthesize title;
@synthesize numofpages;
- (id)initWithTitle:(NSString*) booktitle andNumofpages:(NSNumber*) num{
self = [super init];
if(nil != self)
{
[self setNumofpages:num];
[self setTitle:booktitle];
}
return self;
}
- (NSString*) description{
return title;
}
-(NSString*) summary
{
NSString* retstr = [[NSString alloc]initWithFormat:@"Title: %@, Number of pages: %@",
title, numofpages];
[retstr autorelease];
return retstr;
}
- (void) dealloc{
[numofpages release];
[title release];
[super dealloc];
}
@end
可以看到数据成员title和numofpages的读写函数已经不复存在,取而代之的是两行@synthesize,它让编译器在我们未提供读写函数时自动生成读写函数。
定义了property,客户端可以使用book.title来取代[book title],这种语法比从前更加直观简洁。
实现文件中的16-17行代码可修改如下:
1
2
self.numofpages = num;
self.title = booktitle
注意,许多人很容易忘记上面两行代码中的self,在这种情况下机器生成的读写函数并不会被调用,取而代之的是直接指针赋值,从而会引起内存泄露。
客户端代码如下所示:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#import <Foundation/Foundation.h>
#import "Book.h"
int main (int argc, const char * argv[]) {
NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
NSString* name = [[NSString alloc] initWithString:@"Harry Porter"];
NSNumber* number = [[NSNumber alloc] initWithInt:100];
Book *book = [[Book alloc] initWithTitle:name andNumofpages:number];
[number release];
[name release];
book.title = @"Twilight";
book.numofpages = [NSNumber numberWithInt:200];
NSString* str = book.summary;
NSLog(@"summary: %@", str);
[book release];
[pool drain];
return 0;
}
- Objective C 2.0 简明教程 (5) 属性(Property)
- Objective C 2.0 简明教程 (5) 属性(Property)
- Objective C 2.0 简明教程 (5) 属性(Property)
- Objective C 2.0 属性(Property)
- Objective C 2.0 属性(Property)
- Objective C 2.0 简明教程 (6) Category
- Objective-C 2.0属性(Property)介绍
- objective C property属性详解
- Objective-C 11 属性property
- Objective-C 2.0 @property
- Objective-C 2.0属性(Property) retain和copy的区别
- Objective-C 2.0属性(Property) retain,copy,assign,copy
- objective-c中property的属性
- Objective-C中@property属性使用
- 【Objective-C】@property的所有属性详解
- Objective-C 学习笔记 11 - 属性Property
- iOS Objective-C @property属性的用法
- Objective-C之property属性分析
- 《Moblin非官方教程》系列连载(3)-- 读者对象
- 字符常量与模板匹配问题
- 用于图形绘制的Javascript库jsDraw2D发布
- "update语句的语法错误"解决
- 通过xajax给fckeditor增加自动保存草稿功能
- Objective C 2.0 简明教程 (5) 属性(Property)
- OutputDebugString输出调试信息
- 广州城的十八种性格[转载]
- 《Moblin非官方教程》系列连载(4)-- 发展方向
- Objective C 2.0 简明教程 (6) Category
- MinGW VS CygWin
- 用C#编写的Windows应用程序如何控制TextBox中只能输入数字?
- ★识破庄家的八大武器★
- 《Moblin非官方教程》系列连载(5)-- 开源思想