OC语言@property @synthesize和id
来源:互联网 发布:c语言数据类型范围 编辑:程序博客网 时间:2024/06/02 16:26
OC语言@property @synthesize和id
一、@property @synthesize关键字
注意:这两个关键字是编译器特性,让xcode可以自动生成getter和setter的声明和实现。
(一)@property 关键字
@property 关键字可以自动生成某个成员变量的setter和getter方法的声明
@property int age;
编译时遇到这一行,则自动扩展成下面两句:
- (void)setAge:(int)age;
- (int)age;
(二)@synthesize关键字
@synthesize关键字帮助生成成员变量的setter和getter方法的实现。
语法:@synthesize age=_age;
相当于下面的代码:
- (void)setAge:(int)age
{
_age=age;
}
- (int)age
{
Return _age;
}
(三)关键字的使用和使用注意
类的声明部分:
类的实现部分:
测试程序:
新版本中:
类的声明部分:
类的实现部分:
测试程序:
(1)在老式的代码中,@property只能写在@interface @end中,@synthesize只能写在@implementation @end中,自从xcode 4.4后,@property就独揽了@property和@synthesize的功能。
(2)@property int age;这句话完成了3个功能:1)生成_age成员变量的get和set方法的声明;2)生成_age成员变量set和get方法的实现;3)生成一个_age的成员变量。
注意:这种方式生成的成员变量是private的。
(3)可以通过在{}中加上int _age;显示的声明_age为protected的。
(4)原则:get和set方法同变量一样,如果你自己定义了,那么就使用你已经定义的,如果没有定义,那么就自动生成一个。
(5)手动实现:
1)如果手动实现了set方法,那么编译器就只生成get方法和成员变量;
2)如果手动实现了get方法,那么编译器就只生成set方法和成员变量;
3)如果set和get方法都是手动实现的,那么编译器将不会生成成员变量。
二、Id
id 是一种类型,万能指针,能够指向\操作任何的对象。
注意:在id的定义中,已经包好了*号。Id指针只能指向os的对象。
id 类型的定义
Typedef struct objc object{
Class isa;
} *id;
局限性:调用一个不存在的方法,编译器会马上报错。
- OC语言@property @synthesize和id
- OC语言@property @synthesize和id
- OC语言@property @synthesize和id
- OC语言@property @synthesize和id
- OC语言@property @synthesize和id
- OC语言@property @synthesize和id
- OC语言@property @synthesize和id
- 19.OC语言@property @synthesize和id
- OC语言@property @synthesize和id
- OC语言9-@property @synthesize和id
- 10、黑马程序员-OC语言@property @synthesize和id
- IOS之OC语言@property @synthesize和id
- OC中的@property、@synthesize和id
- OC-@property和@synthesize理解
- OC @property 和 @synthesize 关键字
- OC-@property和@synthesize理解
- OC-@property和@synthesize关键字
- OC之@property和@synthesize
- OC语言构造方法
- log4net 存储到oracle 调试 Could not load type [log4net.Appender.OracleAppender]
- 黑马程序员——Java基础---正则表达式
- 关于块级元素,行级元素和javascript的一些总结
- vss安装及出现弹窗问题
- OC语言@property @synthesize和id
- 协方差和协方差矩阵
- YII框架修改页面布局
- error: failed to attach to process ID
- 解决hibernate+mysql写入数据库乱码
- String.intern()
- mysql 指令笔记
- 联网状态下一条命令配置vim
- Failed to install *.apk on device 'emulator-5554': timeout