Objective-C--@property,@synthesize关键字介绍
Objective-C–@property,@synthesize关键字介绍
转载:http://www.cnblogs.com/QM80/p/3576282.html
/**
注意:由@property声明的属性 在类方法中通过下划线是获取不到的 必须是通过 对象名.属性 才能获取到!~
@property和@synthesize关键字是针对成员变量以及get/set方法而言的
从Xcode4.4以后@property已经独揽了@synthesize的功能主要有三个作用:
(1)生成了成员变量get/set方法的声明
(2)生成了私有的带下划线的的成员变量因此子类不可以直接访问,但是可以通过get/set方法访问。那么如果想让定义的成员变量让子类直接访问那么只能在.h文件中定义成员 变量了,因为它默认是@protected
(3)生成了get/set方法的实现
注意:
如果已经手动实现了get和set方法的话Xcode不会再自动生成带有下划线的私有成员变量了
因为xCode自动生成成员变量的目的就是为了根据成员变量而生成get/set方法的
但是如果get和set方法缺一个的话都会生成带下划线的变量
在Xcode4.4版本之前@property和@synthesize的功能是独立分工的:
@property的作用是:自动的生成成员变量set/get方法的声明如代码:
@property int age; 它的作用和下面两行代码的作用一致
- (void)setAge:(int)age;
- (int)age;
注意:属性名称不要加前缀_ 否则生成的get/set方法中也会有下划线
@synthesize的作用是实现@property定义的方法代码如:
@synthesize age
将@property中定义的属性自动生成get/set的实现方法而且默认访问成员变量age
如果指定访问成员变量_age的话代码如:
@synthesize age = _age;意思是:
把@property中声明的age成员变量生成get/set实现方法,并且在实现方法内部
访问_age这个成员变量,也就意味着给成员 _age 赋值
注意:访问成员变量 _age 如果在.h文件中没有定义_age成员变量的话,就会在.m文件中自动生成@private类型的成员变量_age
*/
Objective-C--@property,@synthesize关键字介绍的更多相关文章
- OC的特有语法-分类Category、 类的本质、description方法、SEL、NSLog输出增强、点语法、变量作用域、@property @synthesize关键字、Id、OC语言构造方法
一. 分类-Category 1. 基本用途:Category 分类是OC特有的语言,依赖于类. ➢ 如何在不改变原来类模型的前提下,给类扩充一些方法?有2种方式 ● 继承 ● 分类(Categor ...
- OC 中 @synthesize 关键字介绍和使用
@synthesize用法 )@property int age; @synthesize age; 表示生成.h中变量 age的 get和 set方法 注意: 如果@synthesize 变量名要先 ...
- OC语言@property @synthesize和id
OC语言@property @synthesize和id 一.@property @synthesize关键字 注意:这两个关键字是编译器特性,让xcode可以自动生成getter和setter的声明 ...
- 李洪强iOS开发之OC语言@property @synthesize和id
OC语言@property @synthesize和id 一.@property @synthesize关键字 注意:这两个关键字是编译器特性,让xcode可以自动生成getter和setter的声明 ...
- 「OC」@property @synthesize和id
一.@property @synthesize关键字 这两个关键字是编译器特性,让Xcode可以自动生成getter和setter. (一)@property 关键字 @property 关键字可以自 ...
- OC @property @synthesize和id
文顶顶 OC语言@property @synthesize和id OC语言@property @synthesize和id 一.@property @synthesize关键字 注意:这两个关键字 ...
- Objective-C 【@property和@synthesize关键字】
------------------------------------------- @property关键字的使用及注意事项 直接上代码和注释了! // //@property关键字的使用 //① ...
- iOS 详细解释@property和@synthesize关键字
/** 注意:由@property声明的属性 在类方法中通过下划线是获取不到的 必须是通过 对象名.属性 才能获取到!- @property和@synthesize关键字是针对成员变量以及get/se ...
- Objective-C 点语法 成员变量的作用域 @property和@synthesize关键字 id类型
点语法 1.利用点语法替换set方法和get方法 方法调用 Student *stu = [Student new]; [stu setAge : 18]; int age = [stu age]; ...
随机推荐
- SEMAT[软件工程方法和理论 Software Engineering Method and Theory]
Agile software development Agile software development is a group of software development methods bas ...
- linux安装python使用的MySQLdb
安装mysqldb模块需已安装mysql 使用pip安装MySQLdb pip install mysql-python mac os安装mysqldb sudo pip install mysql- ...
- 单元测试unit test,集成测试integration test和功能测试functional test的区别
以下内容转自 https://codeutopia.net/blog/2015/04/11/what-are-unit-testing-integration-testing-and-function ...
- JSP 结构
网络服务器需要一个JSP引擎,也就是一个容器来处理JSP页面. 容器负责截获对JSP页面的请求.本教程使用内嵌JSP容器的Apache来支持JSP开发. JSP容器与Web服务器协同合作,为JSP的正 ...
- html5--indexedDB
http://www.cnblogs.com/Johnny_Z/archive/2012/11/04/2753331.html http://database.51cto.com/art/201202 ...
- Book of Evil
Codeforces Round #196 (Div. 2) D:http://codeforces.com/contest/337/status/D 题意:给你一个树,然后树中有一m个点,求到这m个 ...
- Long Long Message
poj2774:http://poj.org/problem?id=2774 题意:求两个串的最长公共子串. 题解:求出后缀数组,然后求height数组,找出最大的值,并且这两个子串在不同的原串中即可 ...
- lc面试准备:Invert Binary Tree
1 题目 Invert a binary tree. 4 / \ 2 7 / \ / \ 1 3 6 9 to 4 / \ 7 2 / \ / \ 9 6 3 1 接口: public TreeNod ...
- [cocos2d demo]新科娘收集水表
讲述的是新科娘在沙滩上遇到一大波水表的故事... 下载地址 链接:http://pan.baidu.com/share/link?shareid=2141087190&uk=293716439 ...
- 怒刷BZOJ记录(二)1038~10xx
我实在是太弱了...不滚粗只能刷BZOJ了...这里来记录每天刷了什么题吧. 2015-8-13: 正式开始! 1030[JSOI2007]文本生成器 | ...