属性的特性

//nonatomic:非原子性(线程不安全的),线程安全会影响系统性能,造成卡顿
//assign:不实行oc中的内存管理机制,通常用于修饰非对象类型,如int float
//retain:实行oc中的内存管理机制,通常用于修饰对象类型,如 NSString
//readonly:只读的,只生成getter方法,不生成setter方法

Car.h
#import <Foundation/Foundation.h>
@interface Car : NSObject

@property (nonatomic,retain) NSString * brand;
@property (nonatomic,assign) NSInteger price;
@property (nonatomic,retain) NSString * color;
+ (instancetype)carWithBrand:(NSString *)brand price:(NSInteger)price color:(NSString *)color;

- (instancetype)initWithBrand:(NSString *)brand price:(NSInteger)price color:(NSString *)color;
@end
Car.m
#import "Car.h"

@implementation Car
//便利构造器
+ (instancetype)carWithBrand:(NSString *)brand price:(NSInteger)price color:(NSString *)color{
    return [[Car alloc]initWithBrand:brand price:price color:color ];
}
//自定义初始化方法
- (instancetype)initWithBrand:(NSString *)brand price:(NSInteger)price color:(NSString *)color{
    self = [super init];
    if (self) {
        _brand = brand;
        _price = price;
        _color = color;
    }
    return self;
}
@end

main.m
    Car *car = [Car carWithBrand:@"BYD" price:120000 color:@"black"];
    NSLog(@"%@,%ld,%@",car.brand,car.price,car.color);
    car.brand = @"BMW";
    NSLog(@"brand:%@",car.brand);                                                                                                                                                                                                                                                                            
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • *面试心声:其实这些题本人都没怎么背,但是在上海 两周半 面了大约10家 收到差不多3个offer,总结起来就是把...
    Dove_iOS阅读 27,211评论 30 472
  • 一、深复制和浅复制的区别? 1、浅复制:只是复制了指向对象的指针,即两个指针指向同一块内存单元!而不复制指向对象的...
    iOS_Alex阅读 1,424评论 1 27
  • __block和__weak修饰符的区别其实是挺明显的:1.__block不管是ARC还是MRC模式下都可以使用,...
    LZM轮回阅读 3,370评论 0 6
  • 提到中国设计,至今很多日本设计名流仍会记起2002年在东京举办的,一场名为“东情西韵”的个人设计展,作品揉合“西方...
    创意造物人阅读 5,411评论 1 4
  • 作为一个中国人,我很骄傲。中华民族的文化和中华民族的精神对我的影响不言而喻。 我读过李白的《望庐山瀑布》,飞流直下...
    不拔电车钥匙的peng阅读 274评论 1 0