Objective-C 学习笔记(四) 已声明的特性 - 推荐
来源:互联网 发布:淘宝类目分析 编辑:程序博客网 时间:2024/05/29 16:22
在面向对象里面,讲究开放-封闭原则,类的成员函数不能被其他类直接访问,通常需要为每个需要访问的变量编写get和set函数,这是个繁琐的事情。Objective-C的这个特性解决了这个问题。
下面通过例子来详解:
声明如下:
@interface TestCoop : NSObject {
int iYear;
int iMonth;
int iDay;
}
@property int iYear; //等同于下面被注释的两句
//- (void) setIYear: (int) iYear;
//-(int) iYear;
- (void) primalSetData: (int)iYear :(int)iMonth :(int)iDay;
- (void) setData: (int)Year iMonth:(int)iMonth iDay:(int)iDay;
- (void) displayDateInfo;
@end
@interface TestAddWeather : TestCoop{
NSString *pstrWeather;
}
@property (copy) NSString *pstrWeather; //等同于下面被注释的两句
//- (void) setPstrWeather: (NSString *) pstrWeather;
//- (NSString *) pstrWeather;
@end
定义如下:
@implementation TestCoop
- (void) displayDateInfo{
NSLog(@"Today is: %d.%d.%d/n", iYear, iMonth, iDay);
}
@synthesize iYear; //等同于下面被注释掉的2个函数
//- (void) setIYear: (int) year{
// iYear = year;
//}
//
//- (int) iYear{
// return iYear;
//}
- (void) primalSetData: (int)year :(int)month :(int)day{
iYear = year;
iMonth = month;
iDay = day;
}
- (void) setData: (int)year iMonth:(int)month iDay:(int)day{
iYear = year;
iMonth = month;
iDay = day;
;
}
@end
@implementation TestAddWeather
@synthesize pstrWeather; //等同于下面被注释掉的2个函数
//- (void) setWeather: (NSString *) pWeather{
// [pstrWeather release];
// pstrWeather = [pWeather copy];
//}
//- (NSString * pWeather) pstrWeather{
// return pstrWeather;
//}
- (void) displayDateInfo{
NSLog(@"Today is: %d.%d.%d,weather: %@/n", iYear, iMonth, iDay, pstrWeather);
}
@end
测试代码:
int main (int argc, const char * argv[]) {
NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
// insert code here...
//NSLog(@"%d-,%s %@/n", 12, "hel123lo", @"123");
TestCoop *ptest = [[TestCoop alloc] init];
[ptest primalSetData :2009 :03 :05];
[ptest displayDateInfo];
[ptest setData:2010 iMonth:06 iDay:06];
[ptest displayDateInfo];
//[ptest setYear:1987];
NSLog(@"now year is %d", [ptest iYear]);
[ptest setIYear:1981];
[ptest displayDateInfo];
[ptest release];
TestAddWeather *ptestweather = [[TestAddWeather alloc] init];
[ptestweather setData:2010 iMonth:03 iDay:05];
//[ptestweather setWeather:@"Rainy"];
[ptestweather setPstrWeather:@"Rainy"];
[ptestweather displayDateInfo];
//[ptestweather setWeather:@"Cloudy"];
//[ptestweather setPstrWeather:@"Cloudy"];
ptestweather.pstrWeather = @"Cloudy";
[ptestweather displayDateInfo];
[ptestweather release];
[pool drain];
return 0;
}
声明部分,可以用@property int iYear;来代替原来的set和get函数的声明,
定义部分,可以用@synthesize iYear;来代替原来set和get函数的定义,
这两个通常要配套使用,如果只写其中一个,另外一部分还是要自己补全的。
在@property 后面,还可以加一些关键字来描述该关键字。
第一类:
Writability
These attributes specify whether ornot a property has an associated set accessor. They are mutually exclusive.
readwrite
Indicatesthat the property should be treated as read/write. This is the default.
Both agetter and setter method will be required in the @implementation.If you use @synthesize in the implementation block, the getter and setter methodsare synthesized.
readonly
Indicatesthat the property is read-only.
If youspecify readonly, only a getter method is required in the @implementation. If you use @synthesize in theimplementation block, only the getter method is synthesized. Moreover, if youattempt to assign a value using the dot syntax, you get a compiler error.
标志只读或者可读写,默认是可读写的,通常在只读的时候才需要加标志
比如:@property (readonly
) int iYear;
第二类:
SetterSemantics
These attributes specify thesemantics of a set accessor. They are mutually exclusive.
assign
Specifiesthat the setter uses simple assignment. This is the default.
Youtypically use this attribute for scalar types such as NSInteger and CGRect, or (in a reference-counted environment) for objects youdon’t own such as delegates.
retain and assign are effectively the same in a garbage-collectedenvironment.
retain
Specifiesthat retain should be invoked on the object upon assignment. (Thedefault is assign.)
Theprevious value is sent a release message.
copy
Specifiesthat a copy of the object should be used for assignment. (The default is assign.)
Theprevious value is sent a release message.
The copyis made by invoking the copy method. This attribute is valid only for object types,which must implement the NSCopying protocol. For furtherdiscussion, see “Copy.”
这一类是表示设置参数时候的方式,默认,拷贝,保留等
copy就是拷贝,可以参考我们上面的代码
retain就是保留,仅维持一个计数
assign是默认的,不需要设置,通常设置int,float之类的变量
第三类:
Atomicity
This attribute specifies that accessor methods are not atomic. (There is no keyword to denote atomic.)
nonatomic
nonatomic
Specifies that accessors are non-atomic. By default, accessors are atomic.
Properties are atomic by default so that synthesized accessors provide robust access to properties in a multi-threaded environment—that is, the value returned from the getter or set via the setter is always fully retrieved or set regardless of what other threads are executing concurrently. For more details, see “Performance and Threading.”
If you do not specify nonatomic, then in a reference counted environment a synthesized get accessor for an object property uses a lock and retains and autoreleases the returned value—the implementation will be similar to the following:
[_internal lock]; // lock using an object-level lock
id result = [[value retain] autorelease];
[_internal unlock];
return result;
If you specify nonatomic, then a synthesized accessor for an object property simply returns the value directly.
如果有一种以上参数,则使用逗号分隔,
比如:
@property(copy, readwrite) NSString *value;
我个人感觉这个特性是相当的好用啊,不用都对不住他。
Objective-C 2.0还提供了点表达式来方便C++的变量访问方式,点表达式如果出现在等号(=)左边,则属性set方法被调用,如果出现在右边,则get方法被调用。
比如:在代码示例中,以下2句是等效的:
[ptestweather setPstrWeather:@"Cloudy"];
ptestweather.pstrWeather = @"Cloudy";
- Objective-C 学习笔记(四) 已声明的特性 - 推荐
- Objective-C基础学习笔记(四)-面向对象的三大特性之继承
- Objective-c 学习笔记(四)
- Objective C学习笔记(四)
- Objective-C学习笔记(2)----Objective-c中@property声明时的参数释疑
- Objective-C学习笔记(十三)——函数的声明与调用
- Objective-C学习笔记第十一章特性
- objective-c学习笔记第八章《objective-c 一些特性》
- [iphone开发]Objective-C学习笔记: Objective-C 语言特性
- 【C++】【学习笔记】/*函数声明的四种方式*/
- iOS学习(三)Objective-C 类的声明
- objective -c 特性笔记
- IOS之Objective-C学习笔记(四)
- Objective-C学习笔记(四)-数据类型和循环结构
- IOS之Objective-C学习笔记(四)
- Objective-C学习笔记四:循环结构
- C学习笔记(四)复杂的变量声明 左右法则
- Objective-C学习笔记(四)——OC实现最简单的数学运算
- 建立个O盘,隐藏你的内容【片片等】cmd命令打jar包
- Java多线程初学者指南(4):线程的生命周期
- Java多线程初学者指南(5):join方法的使用
- string::rfind
- SQL Server TRACE FLAGS
- Objective-C 学习笔记(四) 已声明的特性 - 推荐
- 转牛人牛帖 《或许您还不知道的八款Android开源游戏引擎》
- Java多线程初学者指南(6):慎重使用volatile关键字
- Java多线程初学者指南(7):向线程传递数据的三种方法
- 用spring控制hibernate的session何时关闭
- Java多线程初学者指南(8):从线程返回数据的两种方法
- Android时代的赢创之路
- Map/Reduce中的Combiner的使用
- Java多线程初学者指南(9):为什么要进行数据同步