• @perproty and @synthesize


    1.@property 是什么?
        @perperty 是声明属性的语法,他可以快速方便的为实例变量创建存取器,并允许我们通过点语法使用存取器
        【存取器:用于获取和设置实例变量的方法,获取实例变量值得是getter,设置实例变量存取器的是setter】
        
    2.手工创建存取器
        
        #import <Foundation/Foundation.h>
    
        @interface Car : NSObject
        {
            NSString *carName;
            NSString *carType;
        }
    
        @property(nonatomic,strong) NSString *carName;
        @property(nonatomic,strong) NSString *carType;
        @property(nonatomic,strong) NSString *carNum;
    
    
        @end
        上面的carName和carType就是类Car的属性变量,可以看到分别对这两个实例变量声明了get/set方法,即存取器
        
        #import "Car.h"
    
        @implementation Car
    
        @synthesize carName;
        @synthesize carType;
    
        @end
        以上代码对存取器进行了实现
        
        #import <Foundation/Foundation.h>
        #import "car.h"
        int main(int argc, const char * argv[])
        {
    
            @autoreleasepool {
                
            
                Car *car = [[Car alloc] init];
                car.carName = @"Defuli";
                car.carType = @"SUB";
                car.carNum = @"12";
                NSLog(@"The Car name is %@ and the type is %@ and the No is %@.",car.carName,car.carType,car.carNum);
                
                [car setCarName:@"FUjian"];
                [car setCarType:@"FLL"];
                
                NSLog(@"The Car name is %@ and the type is %@",car.carName,car.carType);
            }
            return 0;
        }
        mian中的实际用法
  • 相关阅读:
    暗影精灵3安装无线网卡驱动(ubuntu16.04)
    装饰器之基本
    pyhton代码规范
    2.线程
    文件拾遗
    闭包函数
    6.文件基本操作
    1.socket网络编程
    9.异常处理
    Python语言规范
  • 原文地址:https://www.cnblogs.com/mypsq/p/5265870.html
Copyright © 2020-2023  润新知