1. 基本数据类型的属性我们不需要管理,我们知需要管理对象类型的属性
(1)变量的写法
@property(nonatomic,assign)CGFloat speed;
@property(nonatomic,copy)NSString *color;
@property(nonatomic,copy)NSString *color;
@property
(
nonatomic
,
retain
)
NSString
*brand;
(2)苹果内部实现 set和get方法
@synthesize color= _color,brand = _brand,speed = _speed;
- (void)setColor:(NSString *)color{
if (_color != color) {
[_color release];
_color = [color copy];
}
}
-(NSString *)color{
return [[_color copy]autorelease];
}
- (void)setBrand:(NSString *)brand{
if (_brand != brand) {
[_brand release];
_brand =[_brand retain];
}
}
- (NSString *)brand{
return [[_brand retain]autorelease];
}
- (void)setSpeed:(CGFloat)speed{
_speed =speed;
}
- (CGFloat)speed{
return _speed;
}
- (void)setColor:(NSString *)color{
if (_color != color) {
[_color release];
_color = [color copy];
}
}
-(NSString *)color{
return [[_color copy]autorelease];
}
- (void)setBrand:(NSString *)brand{
if (_brand != brand) {
[_brand release];
_brand =[_brand retain];
}
}
- (NSString *)brand{
return [[_brand retain]autorelease];
}
- (void)setSpeed:(CGFloat)speed{
_speed =speed;
}
- (CGFloat)speed{
return _speed;
}
- (void)dealloc{
NSLog(@"速度为%.2lf的车被销毁了",_speed);
[_brand release];
[_color release];
[super dealloc];
}
NSLog(@"速度为%.2lf的车被销毁了",_speed);
[_brand release];
[_color release];
[super dealloc];
}
(3)苹果内部实现 便利构造器
//遍历构造器的完整写法
Person *p2 =[Person personWithName:@"zhangyu" age:24 car:c2];
NSLog(@"%@",p2);
+ (id)personWithName:(NSString *)name age:(NSInteger)age car:(Car *)car;
+ (id)personWithName:(NSString *)name age:(NSInteger)age car:(Car *)car{
// Person *p = [[Person alloc]initWithName:name age:age car:car];
[p release];不能立即把创建出来的对象释放掉
// [p autorelease];
//遍历构造器创建出来的对象不能立即释放,需要使用autorelease释放
//注意:既满足了对象的释放,有保证了对象在使用之前不会被释放
return [[[Person alloc]initWithName:name age:age car:car]autorelease];
// [p release];return之后的语句永远都不会执行
}
// Person *p = [[Person alloc]initWithName:name age:age car:car];
[p release];不能立即把创建出来的对象释放掉
// [p autorelease];
//遍历构造器创建出来的对象不能立即释放,需要使用autorelease释放
//注意:既满足了对象的释放,有保证了对象在使用之前不会被释放
return [[[Person alloc]initWithName:name age:age car:car]autorelease];
// [p release];return之后的语句永远都不会执行
}
(4)苹果内部实现 自定义方法
- (id)initWithName:(NSString *)name age:(NSInteger)age car:(Car *)car;
- (id)initWithName:(NSString *)name age:(NSInteger)age car:(Car *)car{
if (self = [super init]) {
self.name = name;
self.age = age;//这个可以不写,因为是基本类型
self.car = car;
}
return self;
}
if (self = [super init]) {
self.name = name;
self.age = age;//这个可以不写,因为是基本类型
self.car = car;
}
return self;
}
多态
1.父类指针指向子类对象,没有继承就没有多态
Animal *animal1 =[[Animal alloc]init];
[animal1 eat];
//编译的时候,animal2是Animal类型的对象,运行的时候发现animal2其实是Cat类型的对象
//猫
Animal *animal2 =[[Cat alloc]init];
[animal1 eat];
//编译的时候,animal2是Animal类型的对象,运行的时候发现animal2其实是Cat类型的对象
//猫
Animal *animal2 =[[Cat alloc]init];
[animal2 eat];
2.多态的局限性:父类类型的指针不能直接调用子类的方法,但是这样写是不规范的,oc是个弱语法,很多情况不会报错,只会发出警告,所以对于我们初学者一定要养成代码规范
3.如果还是想要调用子类独有的方法,此时必须把父类类型的指针强制转换为子类类型
[(Cat *)animal2 catchMouse];
//狗
Animal *animal3 =[[Dog alloc]init];
[animal3 eat];
Person *p3 =[[Person alloc]init];
[p3 feed:animal1];
Dog *dog =[[Dog alloc]init];
//多态的好处:如果一个方法使用的是父类的类型作为参数,此时,可以传进父类对象,也可以传进子类对象
[p3 feed:dog];
//狗
Animal *animal3 =[[Dog alloc]init];
[animal3 eat];
Person *p3 =[[Person alloc]init];
[p3 feed:animal1];
Dog *dog =[[Dog alloc]init];
//多态的好处:如果一个方法使用的是父类的类型作为参数,此时,可以传进父类对象,也可以传进子类对象
[p3 feed:dog];
[p3 release];
[animal3 release];
[animal2 release];
[animal1 release];
[animal3 release];
[animal2 release];
[animal1 release];