`
shuai1234
  • 浏览: 933936 次
  • 性别: Icon_minigender_1
  • 来自: 山西
社区版块
存档分类
最新评论

objective-c中的@property,@synthesize等简易用法(八)

 
阅读更多

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:

 

[plain] view plaincopy
 
  1. #import <Foundation/Foundation.h>  
  2.   
  3. @interface Human : NSObject  
  4. {  
  5.     int age;  
  6.     float height;  
  7. }  
  8. //这里用@property表示,代表了age和height的set,get方法的声明  
  9. @property int age;  
  10. @property float height;  
  11.   
  12. @end  


Human.m:

 

 

[plain] view plaincopy
 
  1. #import "Human.h"  
  2.   
  3. @implementation Human  
  4. //这里用@synthesize表示age,height变量的set,get方法的实现。  
  5. @synthesize age;  
  6. @synthesize height;  
  7.   
  8. @end  


main.m:

 

 

[plain] view plaincopy
 
  1. #import <Foundation/Foundation.h>  
  2. #import "Human.h"  
  3. int main(int argc, const char * argv[])  
  4. {  
  5.     NSAutoreleasePool *pool=[[NSAutoreleasePool alloc]init];  
  6.       
  7.     Human *human = [[Human alloc]init];  
  8.     //“.”在等号左边,相当于set方法。  
  9.       
  10.     human.age=20; //等价于[human setAge:20]  
  11.     human.height=60.50;  
  12.       
  13.     //“.”在等号右边,相当于get方法。  
  14.     float tmp=human.height;//等价于 float tmp = [human height]  
  15.     NSLog(@"年龄%d,身高%f",human.age,tmp);  
  16.       
  17.     [human release];  
  18.     [pool release];//相当于对池中每个对象执行了一次release;  
  19.       
  20.   
  21. }  

 

输出语句:

 

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:

 

[plain] view plaincopy
 
  1. #import <Foundation/Foundation.h>  
  2.   
  3. @interface Human : NSObject  
  4. {  
  5.     int age;  
  6.     float height;  
  7. }  
  8. //这里用@property表示,代表了age和height的set,get方法的声明  
  9. @property (readonly)int age;  
  10. @property float height;  
  11.   
  12. @end  


Human.m:

 

 

[plain] view plaincopy
 
  1. #import "Human.h"  
  2.   
  3. @implementation Human  
  4. //重写init方法给age赋初值  
  5. -(id)init  
  6. {  
  7.     if(self=[super init])  
  8.     {  
  9.         age=20;  
  10.     }  
  11.     return self;  
  12. }  
  13. //这里用@synthesize表示age,height变量的set,get方法的实现。  
  14. @synthesize age;  
  15. @synthesize height;  
  16.   
  17. @end  


main.m:

 

 

[plain] view plaincopy
 
  1. #import <Foundation/Foundation.h>  
  2. #import "Human.h"  
  3. int main(int argc, const char * argv[])  
  4. {  
  5.     NSAutoreleasePool *pool=[[NSAutoreleasePool alloc]init];  
  6.       
  7.     Human *human = [[Human alloc]init];  
  8.     human.height=60.5;  
  9.     //human.age=30; 该行如果不注释是会报错的,即age只有get方法,没有set方法  
  10.     NSLog(@"年龄age=%d",human.age);  
  11.       
  12.      
  13.     [human release];  
  14.     [pool release];//相当于对池中每个对象执行了一次release;  
  15.       
  16.   
  17. }  


输出:

 

2012-03-15 10:32:11.497 String[360:403] 年龄age=20

 

关键字:objective-c ,objective c , oc ,@property , @synthesize

分享到:
评论

相关推荐

    Objective-C中的@property和@synthesize用法详解

    相信每个初学者对@property和@synthesize都感到非常的陌生,在此给大家分享下我的自己的理解,有不当之处,还望多多指教。详细说明文章在下面连接http://blog.csdn.net/comeontom/article/details/7455459

    Objective-c解析XML封装

    @property (nonatomic, retain) NSMutableString *currentResult; @property (nonatomic, retain) NSMutableDictionary *map; @property (nonatomic, retain) NSMutableArray *list; -(NSMutableDictionary *)...

    objective-c小技巧

    1. 使用@property和@synthesize声明一个成员变量,给其赋值是时要在前面加上"self.",以便调用成员变量的setmember方法。 直接调用成员变量并且给其赋值:member=[NSString stringWithFormat:@””];将不执行...

    Objective-C for Absolute Beginners iPhone, iPad, and Mac Programming Made Easy

    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对象组装XML

    [map setObject:@"c" forKey:@"content"]; 或者 NSMutableArray *list = [[NSMutableArray alloc]init]; NSMutableDictionary *map1 = [[NSMutableDictionary alloc]init]; [map1 setObject:@"a1" forKey:@...

    Objective-C for Absolute Beginners: iPhone and Mac Programming Made Easy

    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面试:笑到最后的技术攻略!.zip

    Objective-C、iOS开发、Mac OS X、编程语言、面向对象编程、内存管理、自动引用计数(ARC)、协议(protocol)、类扩展(category)、键值观察(KVO)、键值编码(KVC)、Block、Delegate模式、多态性、Singleton...

    Objective-C for Absolute Beginners(Apress,3ed,2016).pdf

    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 PPT

    本套PPT,拥有以下OC语言的内容: ...二、OC的特有语法(点语法、@property、@synthesize关键字、id、构造方法、分类、description、SEL) 三、内存管理 四、协议(protocol)代码块(block) 五、ARC 等等

    iOS开发中属性 property 和 synthesize 详解

    针对iOS开发中属性 property 和 synthesize 进行了详细介绍

    DT系列伺服马达产品手册.pdf

    DT系列伺服马达产品手册pdf,DT系列伺服马达产品手册

    Python库 | synthesize-0.0.1-py3-none-any.whl

    资源分类:Python库 所属语言:Python 资源全名:synthesize-0.0.1-py3-none-any.whl 资源来源:官方 安装方法:https://lanzao.blog.csdn.net/article/details/101784059

    irrelon-synthesize:一个用于 JavaScript 模块的简单 getter setter 合成器

    自动创建简单的方法来获取和设置 JavaScript 模块的属性,类似于 iOS Objective-C 中的 @synthesize 指令。 用法 在这个例子中,我们在 MyClass 原型上创建了一个名为 name() 的 getter/setter 方法: var MyClass...

    varidict_handler

    变量处理程序一个ObjC助手来使用varidict处理程序。 一行代码值得一千个单词。 @interface Consumer/* * The type is void (^)() or void (^)(id self). */@property ( nonatomic , copy ) NDHandler handler;- ( ...

    Android代码-Synthesize

    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

    3D-Synthesize3DviaDepthOrSil.zip,[CVPR 2017]通过建模多视图深度图或轮廓生成和重建三维形状,3D建模使用专门的软件来创建物理对象的数字模型。它是3D计算机图形的一个方面,用于视频游戏,3D打印和VR,以及其他...

    引导程序:针对高质量编码的iOS项目引导程序

    iOS项目引导您如何设置iOS项目?...代码质量和警告警告是由编译器团队添加的原因,例如,我从Weverything开始并禁用一些警告: Wno-objc-missing-property-synthesis-不想对属性进行@synthesize Wno未使用

    Synthesize & Power Analyze

    Synthesize & Power Analyze

    ObjcAssociatedObjectHelpers:使与关联对象的工作变得更加愉快

    在类别中添加ivars -Obj-C类别的一个不幸缺点是,即使可以添加属性,也无法添加或合成ivars。 关联的对象可用于提供存储并克服此限制: @interface NSObject (MyCategory) @property (strong) id ...

Global site tag (gtag.js) - Google Analytics