当前位置: 首页 > news >正文

ios的@property属性和@synthesize属性(转)

 

当你定义了一系列的变量时,需要写很多的getter和setter方法,而且它们的形式都是差不多的,,所以Xcode提供了@property 和@synthesize属性,@property用在 .h 头文件中用作声明,@synthesize用在.m 文件中用于实现。

如下,新建一个基于“Command Line Tool”的项目,名为“property”,再新建一个Student类,

传统的写法是:

Student.h

[cpp] view plain copy
  1. //  
  2. //  Student.h  
  3. //  property  
  4. //  
  5. //  Created by Rio.King on 13-8-25.  
  6. //  Copyright (c) 2013年 Rio.King. All rights reserved.  
  7. //  
  8.   
  9. #import <Foundation/Foundation.h>  
  10.   
  11. @interface Student : NSObject  
  12. {  
  13.     int age;  
  14.     int no;  
  15. }  
  16.   
  17. //age的getter和setter方法声明  
  18. - (int)age;  
  19. - (void)setAge:(int)newAge;  
  20.   
  21. //no的getter和setter方法声明  
  22. - (int)no;  
  23. - (void)setNo:(int)newNo;  
  24.   
  25. @end  


Student.m

[cpp] view plain copy
  1. //  
  2. //  Student.m  
  3. //  property  
  4. //  
  5. //  Created by Rio.King on 13-8-25.  
  6. //  Copyright (c) 2013年 Rio.King. All rights reserved.  
  7. //  
  8.   
  9. #import "Student.h"  
  10.   
  11. @implementation Student  
  12.   
  13. //age的getter和setter方法的实现  
  14. - (int)age  
  15. {  
  16.     return age;  
  17. }  
  18. -(void)setAge:(int)newAge  
  19. {  
  20.     age = newAge;  
  21. }  
  22.   
  23. //no的getter和setter方法的实现  
  24. - (int)no  
  25. {  
  26.     return no;  
  27. }  
  28. - (void)setNo:(int)newNo  
  29. {  
  30.     no = newNo;  
  31. }  
  32.   
  33. @end  


main.m

[cpp] view plain copy
  1. //  
  2. //  main.m  
  3. //  property  
  4. //  
  5. //  Created by Rio.King on 13-8-25.  
  6. //  Copyright (c) 2013年 Rio.King. All rights reserved.  
  7. //  
  8.   
  9. #import <Foundation/Foundation.h>  
  10. #import "Student.h"   
  11.   
  12. int main(int argc, const char * argv[])  
  13. {  
  14.   
  15.     @autoreleasepool {  
  16.           
  17.         // insert code here...  
  18.         Student *stu = [[Student alloc] init];  
  19.         stu.age = 100;//这句相当于setter方法  
  20.         NSLog(@"age is %i", stu.age);//这里的 stu.age 相当于getter方法  
  21.           
  22.         [stu release];  
  23.           
  24.     }  
  25.     return 0;  
  26. }  


------------------------------------------------------------------------------------------------------------------------ 用@property和@synthesize的写法是:

 Student.h

[cpp] view plain copy
  1. //  
  2. //  Student.h  
  3. //  property  
  4. //  
  5. //  Created by Rio.King on 13-8-25.  
  6. //  Copyright (c) 2013年 Rio.King. All rights reserved.  
  7. //  
  8.   
  9. #import <Foundation/Foundation.h>  
  10.   
  11. @interface Student : NSObject  
  12. {  
  13.     int age;  
  14.     int no;  
  15. }  
  16.   
  17. //当编译器遇到@property时,会自动展开成getter和setter的声明  
  18. @property int age;  
  19. @property int no;  
  20.   
  21.   
  22. @end  


Student.m

[cpp] view plain copy
  1. //  
  2. //  Student.m  
  3. //  property  
  4. //  
  5. //  Created by Rio.King on 13-8-25.  
  6. //  Copyright (c) 2013年 Rio.King. All rights reserved.  
  7. //  
  8.   
  9. #import "Student.h"  
  10.   
  11. @implementation Student  
  12.   
  13. //@synthesize 会自动生成getter和setter的实现  
  14. //@synthesize 默认会去访问age,no,height同名的变量,,  
  15. //如果找不到同名的变量,会在内部自动生成一个私有同名变量age,no,height,,  
  16. //因此Student.h 中的这几个变量也可以省略不写。  
  17. @synthesize age,no;  
  18.   
  19. @end  


main.m

[cpp] view plain copy
  1. //  
  2. //  main.m  
  3. //  property  
  4. //  
  5. //  Created by Rio.King on 13-8-25.  
  6. //  Copyright (c) 2013年 Rio.King. All rights reserved.  
  7. //  
  8.   
  9. #import <Foundation/Foundation.h>  
  10. #import "Student.h"  
  11.   
  12. int main(int argc, const char * argv[])  
  13. {  
  14.       
  15.     @autoreleasepool {  
  16.           
  17.         // insert code here...  
  18.         Student *stu = [[Student alloc] init];  
  19.         stu.age = 100;  
  20.         NSLog(@"age is %i", stu.age);  
  21.           
  22.         [stu release];  
  23.     }  
  24.     return 0;  
  25. }  


几点说明:

1.在Xcode4.5及以后的版本中,可以省略@synthesize ,编译器会自动帮你加上getter 和 setter 方法的实现,并且默认会去访问

_age这个成员变量,如果找不到_age这个成员变量,会自动生成一个叫做 _age私有成员变量。

2.视频教学中建议变量名用"_"前缀作为开头,但我看big Nerd 那本书里是不用的,个人也比较习惯 big Nerd 的那种写法,所以变量名就不加前缀了。Y^o^Y

 

摘自:http://blog.csdn.net/chaoyuan899/article/details/10310719

 

 

转载于:https://www.cnblogs.com/YangBinChina/p/3762253.html

相关文章:

  • 如何在无头模式下运行WebDriver ?
  • C#和Java交互相关研究
  • 以游戏化思维来做运营工作
  • Django分页、模板继承
  • Linux三剑客
  • [Bada开发]初步入口函数介绍
  • WebSocket初探
  • webpack4 一点通
  • 小米抢购神器-开放源码
  • 互联网企业数据安全体系建设
  • 超过响应缓冲区限制
  • 应用监控的选型思考
  • MyEclipse恢复被删除的文件
  • 贺建奎:愿意用自己孩子第一个尝试,研究不小心泄露,英美也有类似实验
  • 线性回归感觉会有用 临时保存,
  • 【翻译】babel对TC39装饰器草案的实现
  • angular学习第一篇-----环境搭建
  • CSS3 变换
  • ES6 学习笔记(一)let,const和解构赋值
  • Java 23种设计模式 之单例模式 7种实现方式
  • java中的hashCode
  • JS数组方法汇总
  • seaborn 安装成功 + ImportError: DLL load failed: 找不到指定的模块 问题解决
  • 翻译--Thinking in React
  • 工作手记之html2canvas使用概述
  • 坑!为什么View.startAnimation不起作用?
  • 如何实现 font-size 的响应式
  • 实战|智能家居行业移动应用性能分析
  • raise 与 raise ... from 的区别
  • ​ 全球云科技基础设施:亚马逊云科技的海外服务器网络如何演进
  • ​configparser --- 配置文件解析器​
  • ​linux启动进程的方式
  • ​创新驱动,边缘计算领袖:亚马逊云科技海外服务器服务再进化
  • ​中南建设2022年半年报“韧”字当头,经营性现金流持续为正​
  • #pragma预处理命令
  • #我与Java虚拟机的故事#连载18:JAVA成长之路
  • $.extend({},旧的,新的);合并对象,后面的覆盖前面的
  • (3)选择元素——(17)练习(Exercises)
  • (MATLAB)第五章-矩阵运算
  • (SpringBoot)第七章:SpringBoot日志文件
  • (待修改)PyG安装步骤
  • (过滤器)Filter和(监听器)listener
  • (汇总)os模块以及shutil模块对文件的操作
  • (论文阅读23/100)Hierarchical Convolutional Features for Visual Tracking
  • (篇九)MySQL常用内置函数
  • (译) 理解 Elixir 中的宏 Macro, 第四部分:深入化
  • (原創) 如何解决make kernel时『clock skew detected』的warning? (OS) (Linux)
  • .NET Standard 的管理策略
  • .NET/C# 利用 Walterlv.WeakEvents 高性能地中转一个自定义的弱事件(可让任意 CLR 事件成为弱事件)
  • .NetCore实践篇:分布式监控Zipkin持久化之殇
  • .NET版Word处理控件Aspose.words功能演示:在ASP.NET MVC中创建MS Word编辑器
  • .net对接阿里云CSB服务
  • :“Failed to access IIS metabase”解决方法
  • @DependsOn:解析 Spring 中的依赖关系之艺术
  • [ JavaScript ] JSON方法