OC语言@property @synthesize和id

一、@property @synthesize

这两个关键字是编译器特性,让xcode可以自动生成gettersetter的声明和实现。

(一)@property 

@property 关键字可以自动生成某个成员变量的settergetter方法的声明

@property int age;

编译时遇到这一行,则自动扩展成下面两句:

- (void)setAge:(int)age;

- (int)age;

(二)@synthesize

@synthesize关键字帮助生成成员变量的settergetter方法的实现。

语法:@synthesize age=_age;

相当于下面的代码:

- setAge:(int)age

_age=age;

- (int)age

Return _age;

(三)关键字的使用和使用注意

类的声明部分:

类的实现部分:

测试程序:

新版本中:

类的声明部分:

类的实现部分:

测试程序:

(1)在老式的代码中,@property@interface  @end@synthesize@implementation   @endxcode 4.4@property@property@synthesize

(2)@property int age;这句话完成了成员变量的方法的声明;方法的实现;)生成一个的成员变量。

 
这种方式生成的成员变量是private

Class isa;

} *id;

局限性:调用一个不存在的方法,编译器会马上报错。

原文地址:https://www.cnblogs.com/zhujungang/p/5125841.html