//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);