@property




  @property



 

 @property 关键字的使用  (编译器指令)

 

 必须与@synthsize关键字匹配使用

 

 (代替了set get 方法的声明)

 

 作用:

 

 xcode4.4之前

 

 编译器遇到 @property 的时候,自动增加 实例变量 get set 方法的

 声明

 

 

 xcode4.4之后

 

 @property 用法得到增强

 

 使用格式

 

 @property  类型  方法名(实例变量名)

 

 @property  int  age;

 

 生成

 

 -(void)setAge:(int)age;   //set方法的声明

 -(int)age;

 


 */




/*

 重写 @property 自动生成的方法

 

 1> 可以单独重写set方法

 2> 可以单独重写get方法

 3> 不可以同时重写两个方法

 

 */

//  @property增强形式


/*

 此时@property可以自动的帮我们生成:

 

 1> 生成一个实例变量 实例变量的名称 _age

 

 2> 生成 实例变量 _age setget方法的声明

 

 3> 生成 实例变量 _age set get 方法的实现

 

 */




#import <Foundation/Foundation.h>


typedef enum{

    kSexMan,

    kSexWoman,

    kSexYao

} Sex;


@interface Person  : NSObject

{

    NSString *_name;

    Sex _sex;

    int _age;

    float _height;

    float _weight;

}

//方法的声明

@property NSString * name;


@property Sex sex;


@property int age;


@property float height;


@end


@implementation Person


-(void)setName:(NSString *) name{

    

    _name = name;

}

-(NSString *)name{

    

    return _name;

}


-(void)setSex:(Sex)sex{

    

    _sex  = sex;

}

-(Sex)sex{

    

    return _sex;

}


-(void)setAge:(int)age{

    

    _age = age;

}

-(int)age{

    

    return _age;

    

}


-(void)setHeight:(float)height{

    

    _height = height;

    

}

-(float)height{

    

    return _height;

    

}

-(void)setWeight:(float)weight{

    

    _weight = weight;

}

-(float)weight{

    

    return _weight;

}


@end

int main(int argc, const char * argv[]) {

    @autoreleasepool {

        

        Person *p = [Person new];

        

        //p.name = @"凤姐";

        

        [p setName:@"凤姐"];

        //                [p setSex:kSexYao];


        p.sex = kSexYao;

        p.weight = 180;

        p.height= 155;

        

        NSLog(@"%@,%d,%.2f,%.2f",p.name,p.sex,p.weight,p.height);

     


    }

    return 0;

}




//  @property增强下重写setget


/*

 

 

 */


#import <Foundation/Foundation.h>



//声明

@interface Person : NSObject


@property int age;


@property NSString *name;


-(void)test;


@end


//实现

@implementation Person


- (void)test

{

    NSLog(@"_age = %d",_age);

}



- (void)setAge:(int)age

{

    if(age > 0){

        

        _age = age;

        

    }

}

@end

int main(int argc, const char * argv[]) {

    @autoreleasepool {

        // 创建一个Person类的对象

        Person *p = [Person new];

        p.age = -1;

        p.name = @"jack";

        [p test];

        

        NSLog(@"Hello, World!    %@",p.name);

    }

    return 0;

}




  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值