zoukankan      html  css  js  c++  java
  • object-c中@property @synthesize的用法

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

    Human.h:

    [plain] view plaincopy
     
    1. #import <Foundation/Foundation.h>  
    2.   
    3. @interface Human : NSObject  
    4. {  
    5.     int age;  
    6.     float height;  
    7. }  
    8. //这里用@property表示,代表了age和height的set,get方法的声明  
    9. @property int age;  
    10. @property float height;  
    11.   
    12. @end  


    Human.m:

    [plain] view plaincopy
     
    1. #import "Human.h"  
    2.   
    3. @implementation Human  
    4. //这里用@synthesize表示age,height变量的set,get方法的实现。  
    5. @synthesize age;  
    6. @synthesize height;  
    7.   
    8. @end  


    main.m:

    [plain] view plaincopy
     
    1. #import <Foundation/Foundation.h>  
    2. #import "Human.h"  
    3. int main(int argc, const char * argv[])  
    4. {  
    5.     NSAutoreleasePool *pool=[[NSAutoreleasePool alloc]init];  
    6.       
    7.     Human *human = [[Human alloc]init];  
    8.     //“.”在等号左边,相当于set方法。  
    9.       
    10.     human.age=20; //等价于[human setAge:20]  
    11.     human.height=60.50;  
    12.       
    13.     //“.”在等号右边,相当于get方法。  
    14.     float tmp=human.height;//等价于 float tmp = [human height]  
    15.     NSLog(@"年龄%d,身高%f",human.age,tmp);  
    16.       
    17.     [human release];  
    18.     [pool release];//相当于对池中每个对象执行了一次release;  
    19.       
    20.   
    21. }  

    输出语句:

    2012-03-15 10:11:34.052 String[325:403] 年龄20,身高60.500000

    这时系统会自动生成age,height的set,get方法,可以直接使用,那还有一种情况,如果我只想让age有get方法,没有set方法怎么办,也就是说在初始化里给一个默认值,而这个值只能被调用,不能被修改,用@property这种方式可以吗?答案是肯定的,因为@property给我们提供了类似参数的一种可选项,关键词是readonly,readwrite,分别对应着只能读不能修改和读写均可,一般我们不设置readwrite,因为默认就是读写状态。看代码:

    Human.h:

    [plain] view plaincopy
     
    1. #import <Foundation/Foundation.h>  
    2.   
    3. @interface Human : NSObject  
    4. {  
    5.     int age;  
    6.     float height;  
    7. }  
    8. //这里用@property表示,代表了age和height的set,get方法的声明  
    9. @property (readonly)int age;  
    10. @property float height;  
    11.   
    12. @end  


    Human.m:

    [plain] view plaincopy
     
    1. #import "Human.h"  
    2.   
    3. @implementation Human  
    4. //重写init方法给age赋初值  
    5. -(id)init  
    6. {  
    7.     if(self=[super init])  
    8.     {  
    9.         age=20;  
    10.     }  
    11.     return self;  
    12. }  
    13. //这里用@synthesize表示age,height变量的set,get方法的实现。  
    14. @synthesize age;  
    15. @synthesize height;  
    16.   
    17. @end  


    main.m:

    [plain] view plaincopy
     
      1. #import <Foundation/Foundation.h>  
      2. #import "Human.h"  
      3. int main(int argc, const char * argv[])  
      4. {  
      5.     NSAutoreleasePool *pool=[[NSAutoreleasePool alloc]init];  
      6.       
      7.     Human *human = [[Human alloc]init];  
      8.     human.height=60.5;  
      9.     //human.age=30; 该行如果不注释是会报错的,即age只有get方法,没有set方法  
      10.     NSLog(@"年龄age=%d",human.age);  
      11.       
      12.      
      13.     [human release];  
      14.     [pool release];//相当于对池中每个对象执行了一次release;  
      15.       
      16.   
      17. }  
  • 相关阅读:
    Android学习之天气预报简单版
    Android学习之Json数据的获取与解析
    getPath()返回路径包含的“%20”(空格)的处理
    自学php 之数据库连接编程
    bnuoj 1071 拼图++
    hdu 2489 Minimal Ratio Tree
    hdu 4720 Naive and Silly Muggles
    hdu 4725 The Shortest Path in Nya Graph
    经典算法学习:排序之希尔排序(壳排序)
    经典算法学习:排序之插入排序
  • 原文地址:https://www.cnblogs.com/zyj442714794/p/4588247.html
Copyright © 2011-2022 走看看