属性声明(property declarations), 自定义属性,自动生成 get 和 set 方法,getter 和 setter
属性声明(property declarations), 自定义属性,自动生成 get 和 set 方法,getter 和 setter |
一、普通的get 和set 属性。 |
一般的get 和set 属性。就是在get 中返回一个变量的值,而在set 属性中给一个变量赋值,一般的我们也把他们称之为接口,用来访问类的私有(private)或者保护(protected)成员。
Circle.h文件
#import <Foundation/Foundation.h>
@interface Circle : NSObject{
int radius;
int x;
int y;
}
-(int)getX;
-(int)getY;
-(int)getRadius;
-(void)setR:(int)_r andX:(int)_x andY:(int) _y;
-(void)setR:(int)_r;
-(void)setY:(int)_y;
-(void)print;
@end
Circle.m文件
#import <Foundation/Foundation.h>
#import "Circle.h"
@implementation Circle
@synthesize radius,x;
@synthesize y;
-(int)getX{
return x;
}
-(int)getY{
return y;
}
-(int)getRadius{
return radius;
}
-(void)setR:(int)_r andX:(int)_x andY:(int) _y{
radius=_r;
x=_x;
y=_y;
}
-(void)setR:(int)_r{
radius=_r;
}
-(void)setY:(int)_y{
y=_y;
}
-(void)print{
NSLog(@"radius = %d ,x = %d , y = %d",[self getRadius],[self getX],[self getY]);
}
@end
二、自动生成 get 和 set 方法 |
自动生成 get 和 set 方法:使用关键字property 。格式:@property type variablesName,...; 一次可以声明多个但是必须是同一种类型。这样在声明之后我们就可以使用 对象.变量名 的格式
来作为 get 和 set 方法,其中有右值得话就是set方法,没有右值就是get方法。
例如
Circle.h文件
#import <Foundation/Foundation.h>
@interface Circle : NSObject{
int radius;
int x;
int y;
}
-(int)getX;
-(int)getY;
-(int)getRadius;
-(void)setR:(int)_r andX:(int)_x andY:(int) _y;
-(void)setR:(int)_r;
-(void)setY:(int)_y;
-(void)print;
//@property (nonatomic)int radius,x,y; //nonatomic的意思是禁止多线程,保护变量,提高性能。
@property (nonatomic)int radius,x;
@property (getter = gy,setter = sy:)int y;
@end
Circle.m文件
#import <Foundation/Foundation.h>
#import "Circle.h"
@implementation Circle
@synthesize radius,x;
@synthesize y;
-(int)getX{
return x;
}
-(int)getY{
return y;
}
-(int)getRadius{
return radius;
}
-(void)setR:(int)_r andX:(int)_x andY:(int) _y{
radius=_r;
x=_x;
y=_y;
}
-(void)setR:(int)_r{
radius=_r;
}
-(void)setY:(int)_y{
y=_y;
}
-(void)print{
NSLog(@"radius = %d ,x = %d , y = %d",self.radius,self.x,[self gy]);
}
@property (nonatomic)int radius,x,y;
@end
main.m文件
#import <Foundation/Foundation.h>
#import "Circle.h"
#import "Person.h"
int main (int argc, const char * argv[])
{
@autoreleasepool {
Circle* c=[[Circle alloc]init];
[c setR:2 andX:3 andY:5];
[c print];
c.radius=10;
c.x=10;
c.y=10;
[c print];
NSLog(@"radius = %d ,x = %d , y = %d ",c.radius,c.x,c.y);
NSLog(@"radius = %d ,x = %d , y = %d",c.radius,c.x,c.y);
c.y=40;
[c y:50];
NSLog(@"y = %d",c.y);
}
return 0;
}
三、getter 和 setter |
getter 和 setter的作用是指定get 和set 属性的名字,给get 和set 属性重命名。在使用getter 和 setter 之后,就不可以继续使用系统自动生成的get 和 set 方法
Circle.h文件
#import <Foundation/Foundation.h>
@interface Circle : NSObject{
int radius;
int x;
int y;
}
-(int)getX;
-(int)getY;
-(int)getRadius;
-(void)setR:(int)_r andX:(int)_x andY:(int) _y;
-(void)setR:(int)_r;
-(void)setY:(int)_y;
-(void)print;
//@property (nonatomic)int radius,x,y;
@property (nonatomic)int radius,x;
@property (getter = gy,setter = sy:)int y;
@end
Circle.m文件
#import <Foundation/Foundation.h>
#import "Circle.h"
@implementation Circle
@synthesize radius,x;
@synthesize y;
-(int)getX{
return x;
}
-(int)getY{
return y;
}
-(int)getRadius{
return radius;
}
-(void)setR:(int)_r andX:(int)_x andY:(int) _y{
radius=_r;
x=_x;
y=_y;
}
-(void)setR:(int)_r{
radius=_r;
}
-(void)setY:(int)_y{
y=_y;
}
-(void)print{
NSLog(@"radius = %d ,x = %d , y = %d",self.radius,self.x,[self gy]);
}
@end
main.m文件 主函数调用
#import <Foundation/Foundation.h>
#import "Circle.h"
#import "Person.h"
int main (int argc, const char * argv[])
{
@autoreleasepool {
Circle* c=[[Circle alloc]init];
[c setR:2 andX:3 andY:5];
[c print];
c.radius=10;
c.x=10;
c.y=10;
[c print];
NSLog(@"radius = %d ,x = %d , y = %d ",c.radius,c.x,c.y);
[c sy:30];
NSLog(@"radius = %d ,x = %d , y = %d",c.radius,c.x,c.y);
c.y=40;
NSLog(@"y= %d",[c gy]);
NSLog(@"y= %d",c.gy);
NSLog(@"y= %d",c.getX);
}
return 0;
}