黑马程序员技术交流社区
标题: @property&@synthesize [打印本页]
作者: 小蜗19910109 时间: 2015-10-18 22:09
标题: @property&@synthesize
四.@property&@synthesize
@property的语法:
使用:
@property int age;
@property特性:
1,@property只能写在@interface @end中。
2,@property 用来自动生成成员变量age的get/set方法声明(xcode4.4以前) 。
3,xcode4.4以后property做了增强 不但帮助我们自动生成get/set方法的声明还帮助我们自动生成get/set方法的实现 。
4,告诉编译器,要生成的get/set方法声明的成员变量类型是int 。
5,如果没有手动声明成员变量,perperty会在.m文件中自动帮我们生成一个开头的成员变量
@synthesize用法:
1. @synthesize age; 表示生成.h中变量age的get和set方法的实现
2. @synthesize 给指定的实例变量赋值。
@synthesize age = _age;
注意;
如果是@synthesize的话, 变量名要先在.h文件中声明。
@property,@synthesize这两个可以配合使用,用于简化set和get方法的定义和实现。
重写setter, getter方法:
只能重写setter, 或者getter方法中的一个
如果重写两个会报错.
欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/) |
黑马程序员IT技术论坛 X3.2 |