OC语言学习 (六) 继承、多态,构造方法,description方法
2014-12-10 18:20
549 查看
声明父类Animal继承自NSObject
Animal.h
Animal.m
子类Dog,继承自Animal
Dog.h
Dog.m
main.m
Animal.h
#ifndef oc_Animal_h #define oc_Animal_h @interface Animal : NSObject { @public int weight; } - (void)eat; //重写默认构造方法 - (id) init; //自定义构造方法 - (id) initWithWeight:(int)newWeight; @end #endif
Animal.m
#import <Foundation/Foundation.h> #import "Animal.h" @implementation Animal - (void)eat { NSLog(@"体重:%d", weight); } //id 是万能指针,可指向任何对象, 不要再加上* //重写默认构造方法init 构造方法:初始化对象 - (id) init { if (self = [super init]) { //!=nil 先初始化父对象,再赋给self, 再初始化自有成员, 最后return self weight = 20; } return self; } //自定义构造方法 - (id) initWithWeight:(int)newWeight { if (self = [super init]) { self->weight = newWeight; } return self; } @end
子类Dog,继承自Animal
Dog.h
#ifndef oc_Dog_h #define oc_Dog_h #import "Animal.h" @interface Dog : Animal { @public int speed; } @property int speed; //自动声明 get set方法 - (void)run; //id 是万能指针,可指向任何对象, 不要再加上* //重写init 构造方法:初始化对象 - (id) init; //重写 NSObject的init - (id) initWithWeightSpeed:(int)newWeight:(int)newSpeed; - (NSString*)description; //对象描述 //重写 NSObject的description + (NSString*)description; //类对象描述 @end #endif
Dog.m
#import <Foundation/Foundation.h> #import "Dog.h" @implementation Dog @synthesize speed; //编译器自动生成get、set的实现方法 - (void)run { NSLog(@"跑-速度:%d", speed); } //id 是万能指针,可指向任何对象, 不要再加上* //重写init 构造方法:初始化对象 - (id) init { if (self = [super init]) { //!=nil speed = 15; //self->speed = 15; } return self; } //自定义构造方法,调用父类自定义构造 - (id) initWithWeightSpeed:(int)newWeight:(int)newSpeed { if (self = [super initWithWeight:newWeight]) { speed = newSpeed; } return self; } - (NSString*)description //对象描述 //重写 NSObject的description { return [NSString stringWithFormat:@"weight=%d,speed=%d", weight, speed]; } + (NSString*)description //类对象描述 { return [NSString stringWithUTF8String:"<Dog : Animal>"]; } @end
main.m
#import "Animal.h" #import "Dog.h" int main() { Animal* animal = [[Animal alloc] init]; //默认构造 [animal eat]; animal = [[Animal alloc] initWithWeight:17]; //自定义构造 [animal eat]; Dog* adog = [[Dog alloc] init];//默认构造 [adog run]; [adog eat]; adog = [[Dog alloc] initWithWeight:16];//父类的自定义构造 [adog eat]; adog = [[Dog alloc] initWithWeightSpeed:16:13];//Dog自身的 自定义构造 [adog run]; [adog eat]; //多态创建对象 Animal* xdog = [[Dog alloc] initWithWeightSpeed:33 :55]; [xdog eat]; Dog* mdog = xdog; [mdog run]; //description方法调用 NSLog([mdog description]); //调用对象方法 NSLog(@"%@", mdog);//输出对象%@ 默认调用description对象方法 NSLog([Dog description]); //调用类方法 NSLog([[Dog class] description]); //默认调用 description类方法 这里省略了@"%@"的format return 0; }
相关文章推荐
- 黑马程序员——OC语言学习——关键字id、构造方法、Category-分类、类的本质、 description方法、 SEL方法
- OC学习日记-继承,多态,私有属性、变量和方法,self,super,属性修饰符,description
- oc语言学习之基础知识点介绍(三):类方法、封装以及继承的介绍
- IOS学习第十三天 OC语言 封装,继承,多态
- oc语言学习之基础知识点介绍(四):方法的重写、多态以及self、super的介绍
- 黑马程序员——OC语言学习——OC面向对象的三大特性:封装、继承、多态,OC字符串——NSString
- OC的特有语法-分类Category、 类的本质、description方法、SEL、NSLog输出增强、点语法、变量作用域、@property @synthesize关键字、Id、OC语言构造方法
- OC学习那点事:构造方法和description方法
- OC语言的第四天 static、self、super、继承、NSObject类、私有属性和私有方法、多态
- JAVA学习笔记(五)继承时的构造方法、多态、接口、汉诺塔
- 第04天OC语言(12):自定义构造方法在继承中的表现
- [黑马IOS自学第十一篇]OC语言基础,SELF关键字,面向对象概念,类学习多态,继承@property等
- OC语言学习——继承和多态的一些随笔记
- OC基础day4-继承,self,super,多态,类对象,SEL类型,description,私有方法与私有变量,三种访问限制
- 黑马程序员之ios学习总结——10 OC语言的构造方法和分类
- OC基础语法学习4:自定义构造方法和description方法
- oc中的构造方法和description方法
- 黑马程序员_OC构造方法和ID类型学习学习
- Java学习记录(一):Java中继承与静态static、非静态、构造方法等的初始化顺序
- 黑马程序员学习笔记14_OC之构造方法