OC学习笔记四(0802 OC8-@property和@synthesize)
来源:互联网 发布:西装p图软件 编辑:程序博客网 时间:2024/05/13 14:10
1. 当编译器遇到 @property时,会自动展开成getter和setter的声明;
@property 只用来声明
@synthesize 只用来实现
@synthesize 写了后,可以不需要写成员变量,会默认帮写上。
@synthesize 默认会去访问跟age同名的变量,如果找不到同名的变量,会自动生成一个私有的同名变量age。
@synthesize生成的成员变量是私有的!
@synthesize会自动生成getter和setter的实现
成员变量一般带下划线
//age = _age 代表getter和setter会去访问_age这个成员变量;即不会再使用age赋值,而是会用_age; age成员变量已经不存在了,不会再生成了。
@synthesize age = _age;
个人觉得这样的作用是,让方法名与成员变量名不一致,成员变量前面带下划线。(@property 声明的setter getter方法名不带下划线)
如果想让成员变量为公有,那么就需要在.h文件里定义成员变量(带下划线的)。
/*************************************/
Xcode 4.5 之后: @synthesize 都省了,只要检测到有@property,编译器就会自动生成@synthersize,意思就是自动生成getter setter方法。
并且,默认生成的成员变量都是下划线开头的,且下划线开始的成员变量是私有的!
如果找不到_age这个成员变量,会自动生成一个叫做_age的私有成员变量。如果也想给子类访问,那么就需要在.h里把这些变量声明一下。
补充:
@property @synthesize 是编译器的一个特性,编译器会帮我们把语法展开
我们自己手动实现setter方法后,编译器就不会再生成setter方法了。
- OC学习笔记四(0802 OC8-@property和@synthesize)
- 黑马视频学习笔记-OC-property、synthesize
- OC学习之@property和@synthesize用法
- OC学习那些事:@property和@synthesize
- OC学习: @property和@synthesize用法概述
- OC学习@property和@synthesize的使用
- OC-@property和@synthesize理解
- OC @property 和 @synthesize 关键字
- OC-@property和@synthesize理解
- OC-@property和@synthesize关键字
- OC之@property和@synthesize
- 【初学】iOS学习笔记-@property @synthesize oc=_oc
- oc学习笔记2—— 点语法 @property @synthesize
- OC学习4——@property和@synthesize
- OC学习篇之---@property和@synthesize的使用
- OC学习小结之@property和@synthesize 关键字
- OC学习篇之---@property和@synthesize的使用
- OC学习篇之---@property和@synthesize的使用
- 一个IT老兵六年的折腾之路!
- Hibernate学习(3)框架简述
- 机房重构之文档——重拾UML(一)
- 常见UML和建模工具比较
- ABP分层架构
- OC学习笔记四(0802 OC8-@property和@synthesize)
- linux虚拟机下Hadoop集群分布式配置
- 别动辄降级论:互联网很伟大 从业者很平凡
- hdu1452(数论入门)
- n&(n-1)的用法
- 【leetCode】Valid Anagram
- jquery里的dialog对话框插件为框架页(iframe) 的方法
- 前端工程师知识体系
- LOG4J