Objective-C中的@property和@synthesize用法详解

相信每个初学者对于这两个东西感觉非常陌生,当然啦,我也是初学者,所以呆会在和大家说这两个东西的时候,希望大家不用要求太高,还有,请大神们绕路哈!

在objective-c中,我们可以用new简单的代替alloc init,我们今天介绍的是类似于new这种简易用法的另一种OC特性,用@property,@synthesize来代替get,set方法,用起来很简单,可以省掉很多的代码量,当需要用SET,GET方法的地方,我们可以用@property,@synthesize来简单的代替,这时系统会自动给我们生成该变量的set,get方法,@property对应方法的声明部分,@synthesize对应方法的实现部分。
Human.h
//
//  Human.h
//  @property@synthesize
//
//  Created by b126 on 12-4-12.
//  Copyright (c) 2012年 __MyCompanyName__. All rights reserved.
//

#import <Foundation/Foundation.h>

@interface Human : NSObject
{
    //int age;
    float height;
    int ear;
    float eyes;
    NSString *leftHand;
    NSString *rightHand;
}
/*这里用@property表示,代表了age和height的set,get方法的声明
 这相当于
 @property(readwrite) int age;
 @property(readwrite) float height;
*/
@property int age;/*这里相当于作如下声明:
                   - (int)age;  
                   -(void)setAge:(int)newAge;  */
@property float height;//同上


//这里用@property表示,代表了ear的get方法的声明
@property(readonly) int ear;/*这里相当于作如下声明:
                             - (int)ear;
                             */

//assign: 默认类型,setter方法直接赋值,而不进行retain操作
@property(assign) float eyes;

//
@property(copy) NSString *leftHand;


@property(retain) NSString *rightHand;
@end

Human.m
//
//  Human.m
//  @property@synthesize
//
//  Created by b126 on 12-4-12.
//  Copyright (c) 2012年 __MyCompanyName__. All rights reserved.
//

#import "Human.h"

@implementation Human
//这里用@synthesize表示age,height变量的set,get方法的实现。
@synthesize age;
                /*相当于这样实现方法
                 - (int)age  
                 {  
                 return age;  
                 }  
                 -(void)setAge:(int)newAge 
                 {  
                 age = newAge;  
                 }*/
/*如何这样写,那么age返回值就会是输入的两倍
 -(int)age{
 return age * 2;
 }*/
@synthesize height;//同上


//这里@synthesize表示ear变量的get方法的实现
@synthesize ear;/* 相当于这样实现代码
                 - (int)ear
                 */

@synthesize eyes;

@synthesize leftHand;

@synthesize rightHand;

//重写init方法给age赋初值
-(id)init
{
    if(self=[super init])
    {
        ear=20;
    }
    return self;
}
@end
main.m
//
//  main.m
//  @property@synthesize
//
//  Created by b126 on 12-4-12.
//  Copyright (c) 2012年 __MyCompanyName__. All rights reserved.
//

#import <Foundation/Foundation.h>
#import "Human.h"

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

    @autoreleasepool {
        
        // insert code here...
        Human *human = [[Human alloc]init];
        
        //“.”在等号左边,相当于set方法。
        human.age=20; //等价于[human setAge:20]
        //[human setAge:80];
        human.height=60.50;
        //[human setHeight:65.99];
        
        //“.”在等号右边,相当于get方法。
        float tmp=human.height;//等价于 float tmp = [human height]
        //tmp = [human height];
        NSLog(@"年龄%d,身高%f",human.age,tmp);
        
        //ear 为readonly,不能在这里给它赋值
        NSLog(@"耳朵是:%d",[human ear]);
        
        [human setEyes:60.5];
        //human.eyes = 61.22;
        NSLog(@"眼睛%f",[human eyes]);
        
        [human setRightHand:@"I'm right hand!"];
        NSLog(@"%@",[human rightHand]);
        
    }
    return 0;
}


完整代码下载: http://download.csdn.net/detail/comeontom/4221585


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值