OC语言@property @synthesize和id
一、@property @synthesize关键字
注意:这两个关键字是便以其特性,让Xcode可以自动生成getter和setter的声明和实现。
(一)@property 关键字
@property 关键字可以自动生成某个成员变量的setter和戈塔特人方法的声明
@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;}
(三)关键字的使用和使用注意
类的声明部分:
#import <Foundation/Foundation.h>
@interface Person : NSObject
// 使用@property关键字实现_age成员变量的set和get方法的声明
@property int age;
@end
类的实现部分:
#import "Person.h"
@implementation Person
// 使用@synthesize关键字实现_age成员变量的set和get方法的声明
@synthesize age = _age;
// 左边的age代表要把@property int age实现
// 右边的——age代表着访问_age这个成员变量
@end
测试程序
#import <Foundation/Foundation.h>
#import "Person.h"
int main(int argh, const char * argue[])
{
Person *p =[ [Person alloc] init];
[p setAge:10];
</pre></div></div><div><div><pre name="code" class="objc">// 使用点语法,打印成员变量的值以验证
// 点语法的本质就是调用set和get方法
NSLog(@"age = %d", p.age);
</pre></div></div><div><div><pre name="code" class="objc">// NSLog(@"age = %d", p-> age); 要求@plubic属性
return 0;
}
新版本中:
类的声明部分:
#Import <Foundation/Foundation.h>
@interface Person : NSObject
{
// 类的成员变量,这样的方式默认认为protected
// 如果这里不写成成员变量,那么会自动生成,但是private的
int _age;
double _weight;
double _height;
NSString *_name;
}
// 使用@property关键字生成成员变量的set和get方法的声明和实现
@property int age;
@property double weight;
@property double height;
@property NSString *name;
@end
类的实现部分:
#import "Person.h"
@implementation Person
// 从Xcode4.4以后,@property关键字独揽了三个功能
@end
测试程序:
#import <Foundation/Foundation.h>
#import "Person.h"
int main(int argh, const char * argue[])
{
// person类型的指针指向一个person类型创建的对象
Person *p = [[Person alloc] init];
// 使用点语法为_age成员变量赋值
p.age = 22;
// 使用set方法成为成员变量赋值
[p setHeight:169];
[p setWeight:50];
[p setName:@yangyong"];
// 使用点语法,本质是调用get方法
NSLog(@"age = %d, height = %f, weight = %f, name = %@", p.age, p.height, p.weight, p.name);
return 0;
}
(1)在老式的代码中,@property只能卸载@interface @end中, @synthesize只能卸载@implementation @end中,自从Xcode4.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方法都是手动实现的,那么编译器将不会生成成员变量。
#import <Foundation/Foundation.h>
@interface Dog : NSObject
{
// 可以省略这里的成员变量
int _age;
}
// 使用@property关键字生成成员变量set和get方法的声明和实现
@property int age;
@end
@implementation Dog
// 自定义了成员变量的返回方法
- (int)age
{
return 10;
}
@end
int main(int argh, const char * argue[])
{
Dog *d = [[Dog alloc] init];
//使用点语法调用set方法设置age的值为5
// 在这里调用 set方法,因为没有,所以编译器在编译时自动生成了。
d.age = 5;
// 因为自己自定义了get方法,所以Xcode尊重用户的选择,请注意输出结果。
NSLog(@"%d", d.age);
return 0;
}
id
id 是一种类型,万能指针,能够指向\操作任何的对象。
注意:在id的定义中,已经包好了*号。id指针只能指向os的对象。
id类型的定义
Typedef struct objc object{
Class isa;
}*id;
局限性:调用一个不存在的方法,编译器会马上报错。