OC2-重写
// // Dog.h // OC2-重写 // // Created by qianfeng on 15/6/17. // Copyright (c) 2015年 qianfeng. All rights reserved. // #import "Animal.h" @interface Dog : Animal { float _weight; } @property (assign,nonatomic)float weight; @end // // Dog.m // OC2-重写 // // Created by qianfeng on 15/6/17. // Copyright (c) 2015年 qianfeng. All rights reserved. // #import "Dog.h" //重写父类的继承过来的方法,不需要再次声明 @implementation Dog -(void)printAnimalInformation { NSLog(@"name=%@,age=%i,,weight=%.2f",_name,_age,_weight); } -(void)showAnimalFood { NSLog(@"%@_____",[self class]); } @end
// // Animal.h // OC2-重写 // // Created by qianfeng on 15/6/17. // Copyright (c) 2015年 qianfeng. All rights reserved. // #import <Foundation/Foundation.h> @interface Animal : NSObject { NSString *_name; int _age; } @property (copy,nonatomic)NSString *name; @property(assign,nonatomic)int age; -(void)showAnimalFood; -(void)printAnimalInformation; @end // // Animal.m // OC2-重写 // // Created by qianfeng on 15/6/17. // Copyright (c) 2015年 qianfeng. All rights reserved. // #import "Animal.h" @implementation Animal -(void)showAnimalFood { NSLog(@"%@_____",[self class]); } -(void)printAnimalInformation { NSLog(@"name=%@,age=%i",_name,_age); } @end
// // main.m // OC2-重写 // // Created by qianfeng on 15/6/17. // Copyright (c) 2015年 qianfeng. All rights reserved. // #import <Foundation/Foundation.h> #import "Dog.h" #import "Animal.h" int main(int argc, const char * argv[]) { @autoreleasepool { Dog *xiaobai =[[Dog alloc] init]; xiaobai.name=@"121"; xiaobai.age=12; xiaobai.weight=12; [xiaobai printAnimalInformation]; [xiaobai showAnimalFood]; Animal *xiaohei =[[Animal alloc] init]; xiaohei.name=@"xiaohei"; xiaohei.age=11; [xiaohei printAnimalInformation]; [xiaohei showAnimalFood]; } return 0; }