黑马程序员——Object-C基础(六)面向对象的特性:继承
2015-03-19 17:06
417 查看
------Java培训、Android培训、iOS培训、.Net培训、期待与您交流! -------
1. 继承的基本用法
例子:写一个矩形类,在写一个继承它的正方形类:#import <Foundation/NSObject.h> @interface MyRectangle: NSObject { int width; int height; } -(MyRectangle*) initWithWidth: (int) w height: (int) h; -(void) setWidth: (int) w; -(void) setHeight: (int) h; -(void) setWidth: (int) w height: (int) h; -(int) width; -(int) height; -(void) print; @end //2. MyRectangle.m #import "MyRectangle.h" #import <stdio.h> @implementation MyRectangle -(MyRectangle*) initWithWidth: (int) w height: (int) h { self = [super init]; if ( self ) { [self setWidth: w height: h]; } return self; } -(void) setWidth: (int) w { width = w; } -(void) setHeight: (int) h { height = h; } -(void) setWidth: (int) w height: (int) h { width = w; height = h; } -(int) width { return width; } -(int) height { return height; } -(void) print { printf( "width = %i, height = %i", width, height ); } @end //3. Square.h #import "MyRectangle.h" @interface Square: MyRectangle -(Square*) initWithSize: (int) s; -(void) setSize: (int) s; -(int) size; @end //4. Square.m #import "Square.h" @implementation Square -(Square*) initWithSize: (int) s { self = [super init]; if ( self ) { [self setSize: s]; } return self; } -(void) setSize: (int) s { width = s; height = s; } -(int) size { return width; } -(void) setWidth: (int) w { [self setSize: w]; } -(void) setHeight: (int) h { [self setSize: h]; } @end //5. main.m #import "Square.h" #import "MyRectangle.h" #import <stdio.h> int main( int argc, const char *argv[] ) { MyRectangle *rec = [[MyRectangle alloc] initWithWidth: 10 height: 20]; Square *sq = [[Square alloc] initWithSize: 15]; // print em printf( "MyRectangle: " ); [rec print]; printf( "\n" ); printf( "Square: " ); [sq print]; printf( "\n" ); // update square [sq setWidth: 20]; printf( "Square after change: " ); [sq print]; printf( "\n" ); //调用继承自父类的width和height printf( "Square after width: %i\n", [sq width] ); printf( "Square after height: %i", [sq height] ); // free memory [rec release]; [sq release]; return 0; } 2. 编译运行: gcc -fconstant-string-class=NSConstantString -c main.m -I /GNUstep/System/Library/Headers gcc -c MyRectangle.m -I /GNUstep/System/Library/Headers gcc -c Square.m -I /GNUstep/System/Library/Headers gcc main.o Square.o MyRectangle.o -o main -L /GNUstep/System/Library/Libraries/ -lobjc -lgnustep-base $ ./main.exe MyRectangle: width = 10, height = 20 Square: width = 15, height = 15 Square after change: width = 20, height = 20 Square after width: 20Square after height: 20
子类方法和属性的访问过程:如果子类没有,就去访问父类的
父类被继承了还是能照常使用的
父类的静态方法
画继承结构图,从子类抽取到父类
NSObject的引出:全部OC类的最终父类,包含了一些常用方法,比如+new
2. 继承的专业术语
父类\超类 superclass子类 subclass\subclasses
3. 继承的细节
单继承子类和父类不能有相同的成员变量
方法的重写
4. super关键字
分别调用父类的对象方法和类方法5. 继承的好处
不改变原来模型的基础上,拓充方法建立了类与类之间的联系
抽取了公共代码
坏处:耦合性强
6. 继承的使用场合
它的所有属性都是你想要的,一般就继承它的部分属性是你想要的,可以抽取出另一个父类
相关文章推荐
- 黑马程序员——Object-C基础(五)面向对象的特性:封装
- 黑马程序员——Object-C基础(七)面向对象的特性:多态
- 黑马程序员——OC语言基础——面向对象三大特性之继承
- 黑马程序员——OC语言基础:面向对象三大特性,封装、继承、多态
- 黑马程序员----------面向对象三大特性之继承
- 黑马程序员_Java基础_面向对象(多态、Object类相关方法)
- 黑马程序员------object-c面向对象_继承
- 黑马程序员_OC基础04_面向对象三大特性
- 黑马程序员-④面向对象三大特性封装、继承、多态
- 黑马程序员——OC面向对象三大特性——封装,继承,多态。
- 黑马程序员——OC基础——面向对象的三大特性(三)
- 黑马程序员 07 Java基础教学 - 07 - 面向对象(3) 之 继承、抽象、接口
- 黑马程序员-(8)Java基础之面向对象(三)继承
- 黑马程序员——OC基础——面向对象的三大特性(一)
- Objective-C基础(面向对象--下--三大特性--继承、多态)
- 黑马程序员_iOS开发之OC之面向对象之三大特性封装、继承和多态
- 黑马程序员——java基础学习(面向对象,继承)
- Java基础<六>---> 面向对象与java特性(封装、继承、多态
- 黑马程序员——OC篇(二)面向对象的三大特性(封装、继承、多态)
- 黑马程序员_Java基础_面向对象,封装,继承,单例设计模式,构造函数,构造代码块