holydancer原创,如需转载,请在显要位置注明:
转自holydancer的CSDN专栏,原文地址:http://blog.csdn.net/holydancer/article/details/7355833
在objective-c中,我们可以用new简单的代替alloc init,我们今天介绍的是类似于new这种简易用法的另一种OC特性,用@property,@synthesize来代替get,set方法,用起来很简单,可以省掉很多的代码量,当需要用SET,GET方法的地方,我们可以用@property,@synthesize来简单的代替,这时系统会自动给我们生成该变量的set,get方法,@property对应方法的声明部分,@synthesize对应方法的实现部分。
Human.h:
- #import <Foundation/Foundation.h>
- @interface Human : NSObject
- {
- int age;
- float height;
- }
- //这里用@property表示,代表了age和height的set,get方法的声明
- @property int age;
- @property float height;
- @end
Human.m:
- #import "Human.h"
- @implementation Human
- //这里用@synthesize表示age,height变量的set,get方法的实现。
- @synthesize age;
- @synthesize height;
- @end
main.m:
- #import <Foundation/Foundation.h>
- #import "Human.h"
- int main(int argc, const char * argv[])
- {
- NSAutoreleasePool *pool=[[NSAutoreleasePool alloc]init];
- Human *human = [[Human alloc]init];
- //“.”在等号左边,相当于set方法。
- human.age=20; //等价于[human setAge:20]
- human.height=60.50;
- //“.”在等号右边,相当于get方法。
- float tmp=human.height;//等价于 float tmp = [human height]
- NSLog(@"年龄%d,身高%f",human.age,tmp);
- [human release];
- [pool release];//相当于对池中每个对象执行了一次release;
- }
输出语句:
2012-03-15 10:11:34.052 String[325:403] 年龄20,身高60.500000
这时系统会自动生成age,height的set,get方法,可以直接使用,那还有一种情况,如果我只想让age有get方法,没有set方法怎么办,也就是说在初始化里给一个默认值,而这个值只能被调用,不能被修改,用@property这种方式可以吗?答案是肯定的,因为@property给我们提供了类似参数的一种可选项,关键词是readonly,readwrite,分别对应着只能读不能修改和读写均可,一般我们不设置readwrite,因为默认就是读写状态。看代码:
Human.h:
- #import <Foundation/Foundation.h>
- @interface Human : NSObject
- {
- int age;
- float height;
- }
- //这里用@property表示,代表了age和height的set,get方法的声明
- @property (readonly)int age;
- @property float height;
- @end
Human.m:
- #import "Human.h"
- @implementation Human
- //重写init方法给age赋初值
- -(id)init
- {
- if(self=[super init])
- {
- age=20;
- }
- return self;
- }
- //这里用@synthesize表示age,height变量的set,get方法的实现。
- @synthesize age;
- @synthesize height;
- @end
main.m:
- #import <Foundation/Foundation.h>
- #import "Human.h"
- int main(int argc, const char * argv[])
- {
- NSAutoreleasePool *pool=[[NSAutoreleasePool alloc]init];
- Human *human = [[Human alloc]init];
- human.height=60.5;
- //human.age=30; 该行如果不注释是会报错的,即age只有get方法,没有set方法
- NSLog(@"年龄age=%d",human.age);
- [human release];
- [pool release];//相当于对池中每个对象执行了一次release;
- }
输出:
2012-03-15 10:32:11.497 String[360:403] 年龄age=20
关键字:objective-c ,objective c , oc ,@property , @synthesize
相关推荐
相信每个初学者对@property和@synthesize都感到非常的陌生,在此给大家分享下我的自己的理解,有不当之处,还望多多指教。详细说明文章在下面连接http://blog.csdn.net/comeontom/article/details/7455459
@property (nonatomic, retain) NSMutableString *currentResult; @property (nonatomic, retain) NSMutableDictionary *map; @property (nonatomic, retain) NSMutableArray *list; -(NSMutableDictionary *)...
1. 使用@property和@synthesize声明一个成员变量,给其赋值是时要在前面加上"self.",以便调用成员变量的setmember方法。 直接调用成员变量并且给其赋值:member=[NSString stringWithFormat:@””];将不执行...
Using a hands-on approach, you'll learn how to think in programming terms, how to use Objective-C to construct program logic, and how to synthesize it all into working apps. Gary Bennett, an ...
[map setObject:@"c" forKey:@"content"]; 或者 NSMutableArray *list = [[NSMutableArray alloc]init]; NSMutableDictionary *map1 = [[NSMutableDictionary alloc]init]; [map1 setObject:@"a1" forKey:@...
Using a hands-on approach, you'll learn how to think in programming terms, how to use Objective-C to construct program logic, and how to synthesize it all into working apps. Gary Bennett, an ...
Objective-C、iOS开发、Mac OS X、编程语言、面向对象编程、内存管理、自动引用计数(ARC)、协议(protocol)、类扩展(category)、键值观察(KVO)、键值编码(KVC)、Block、Delegate模式、多态性、Singleton...
Using a hands-on approach, you’ll learn how to think in programming terms, how to use Objective-C to construct program logic, and how to synthesize it all into working apps. Gary Bennett, an ...
本套PPT,拥有以下OC语言的内容: ...二、OC的特有语法(点语法、@property、@synthesize关键字、id、构造方法、分类、description、SEL) 三、内存管理 四、协议(protocol)代码块(block) 五、ARC 等等
针对iOS开发中属性 property 和 synthesize 进行了详细介绍
DT系列伺服马达产品手册pdf,DT系列伺服马达产品手册
资源分类:Python库 所属语言:Python 资源全名:synthesize-0.0.1-py3-none-any.whl 资源来源:官方 安装方法:https://lanzao.blog.csdn.net/article/details/101784059
自动创建简单的方法来获取和设置 JavaScript 模块的属性,类似于 iOS Objective-C 中的 @synthesize 指令。 用法 在这个例子中,我们在 MyClass 原型上创建了一个名为 name() 的 getter/setter 方法: var MyClass...
变量处理程序一个ObjC助手来使用varidict处理程序。 一行代码值得一千个单词。 @interface Consumer/* * The type is void (^)() or void (^)(id self). */@property ( nonatomic , copy ) NDHandler handler;- ( ...
Synthesize Synthesize is an android library which can create layout images in background threads, services, etc without inflating them in activity or fragments. Synthesize Documentation
3D-Synthesize3DviaDepthOrSil.zip,[CVPR 2017]通过建模多视图深度图或轮廓生成和重建三维形状,3D建模使用专门的软件来创建物理对象的数字模型。它是3D计算机图形的一个方面,用于视频游戏,3D打印和VR,以及其他...
iOS项目引导您如何设置iOS项目?...代码质量和警告警告是由编译器团队添加的原因,例如,我从Weverything开始并禁用一些警告: Wno-objc-missing-property-synthesis-不想对属性进行@synthesize Wno未使用
Synthesize & Power Analyze
在类别中添加ivars -Obj-C类别的一个不幸缺点是,即使可以添加属性,也无法添加或合成ivars。 关联的对象可用于提供存储并克服此限制: @interface NSObject (MyCategory) @property (strong) id ...