【学习笔记】【OC语言】继承

王朝学院·作者佚名  2016-08-27
窄屏简体版  字體: 小  |  中  |  大  |  超大  

1.继承的基本用法

1#import<Foundation/Foundation.h>2/*31.继承的好处:41> 抽取重复代码52> 建立了类之间的关系63> 子类可以拥有父类中的所有成员变量和方法782.注意点91> 基本上所有类的根类是NSObject10*/111213/********Animal的声明*******/14@interfaceAnimal : NSObject15{16int_age;17double_weight;18}1920- (void)setAge:(int)age;21- (int)age;2223- (void)setWeight:(double)weight;24- (double)weight;25@end2627/********Animal的实现*******/28@implementationAnimal29- (void)setAge:(int)age30{31_age =age;32}33- (int)age34{35return_age;36}3738- (void)setWeight:(double)weight39{40_weight =weight;41}42- (double)weight43{44return_weight;45}46@end4748/********Dog*******/49//: Animal 继承了Animal,相当于拥有了Animal里面的所有成员变量和方法50//Animal称为Dog的父类51//Dog称为Animal的子类52@interfaceDog : Animal53@end5455@implementationDog56@end5758/********Cat*******/59@interfaceCat : Animal60@end6162@implementationCat63@end6465intmain()66{67Dog *d = [Dognew];6869[d setAge:10];7071NSLog(@"age=%d", [d age]);72return0;73}

子类方法和属性的访问过程:如果子类没有,就去访问父类的

父类被继承了还是能照常使用的

父类的静态方法

画继承结构图,从子类抽取到父类

NSObject的引出:全部OC类的最终父类,包含了一些常用方法,比如+new

2.继承的专业术语

父类\超类 superclass

子类 subclass\subclasses

3.继承的细节

单继承

子类和父类不能有相同的成员变量

方法的重写

4.super关键字

分别调用父类的对象方法和类方法

1/*2僵尸34跳跃僵尸、舞王僵尸、铁桶僵尸5*/6#import<Foundation/Foundation.h>78/*9super的作用101.直接调用父类中的某个方法112.super处在对象方法中,那么就会调用父类的对象方法12super处在类方法中,那么就会调用父类的类方法13143.使用场合:子类重写父类的方法时想保留父类的一些行为15*/1617//僵尸18@interfaceZoombie : NSObject19- (void)walk;2021+ (void)test;22- (void)test;2324@end2526@implementationZoombie27- (void)walk28{29NSLog(@"往前挪两步******");30}3132+ (void)test33{34NSLog(@"Zoombie+test");35}3637- (void)test38{39NSLog(@"Zoombie-test");40}41@end4243//跳跃僵尸44@interfaceJumpZoombie : Zoombie45+ (void)haha;46- (void)haha2;47@end484950@implementationJumpZoombie5152+ (void)haha53{54[super test];55}5657- (void)haha258{59[super test];60}6162- (void)walk63{64//跳两下65NSLog(@"跳两下");6667//走两下(直接调用父类的walk方法)68[super walk];69//NSLog(@"往前挪两步----");7071}72@end7374intmain()75{76//[JumpZoombie haha];77JumpZoombie *jz = [JumpZoombienew];7879[jz haha2];8081return0;82}

5.继承的好处

不改变原来模型的基础上,拓充方法

建立了类与类之间的联系

抽取了公共代码

坏处:耦合性强

6.继承的使用场合

它的所有属性都是你想要的,一般就继承

它的部分属性是你想要的,可以抽取出另一个父类

7.代码

1/*21.重写:子类重新实现父类中的某个方法,覆盖父类以前的做法32.注意41> 父类必须声明在子类的前面52> 子类不能拥有和父类相同的成员变量63> 调用某个方法时,优先去当前类中找,如果找不到,去父类中找782.坏处:耦合性太强9*/1011#import<Foundation/Foundation.h>12//Person13@interfacePerson : NSObject14{15int_age;16}1718- (void)setAge:(int)age;19- (int)age;2021- (void)run;2223+ (void)test;2425@end2627@implementationPerson2829+ (void)test30{31NSLog(@"Person+test");32}3334- (void)run35{36NSLog(@"person---跑");37}3839- (void)setAge:(int)age40{41_age =age;42}43- (int)age44{45return_age;46}47@end4849//不允许子类和父类拥有相同名称的成员变量50//Student51@interfaceStudent : Person52{53int_no;54//int _age;55}5657+ (void)test2;5859@end6061@implementationStudent62//重写:子类重新实现父类中的某个方法,覆盖父类以前的做法63- (void)run64{65NSLog(@"student---跑");66}6768+ (void)test269{70[self test];71}72@end737475intmain()76{77[Student test2];7879//Student *s = [Student new];80//81//[s run];8283return0;84}

继承的使用场合

1/*21.继承的使用场合31> 当两个类拥有相同属性和方法的时候,就可以将相同的东西抽取到一个父类中42> 当A类完全拥有B类中的部分属性和方法时,可以考虑让B类继承A类5A6{7int _age;8int _no;9}1011B : A12{13int _weight;14}1516// 继承:xx 是 xxx17// 组合:xxx 拥有 xxx18192.组合20A21{22int _age;23int _no;24}2526B27{28A *_a;29int _weight;30}31*/3233@interfaceScore : NSObject34{35int_cScore;36int_ocScore;37}38@end3940@implementationScore41@end4243@interfaceStudent : NSObject44{45//组合46Score *_score;47//int _cScore;48//int _ocScore;49int_age;50}51@end5253@implementationStudent5455@end

 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
 
© 2005- 王朝網路 版權所有 導航