object-c的继承
生活随笔
收集整理的這篇文章主要介紹了
object-c的继承
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
作為面向?qū)ο蟮木幊陶Z言,object-c當(dāng)然也支持繼承,而且和java一樣,支持的是單一繼承,即只有一個超類。
當(dāng)然,也支持覆蓋(或重寫)父類的同名方法。例子如下:
?1. 代碼:
? //1. MyRectangle.h
#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" );
? ? //調(diào)用繼承自父類的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. 編譯運(yùn)行 :
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
? ?(1) Square繼承自MyRectangle,重寫了setWidth和setHeight方法;
? ?(2) Square可以調(diào)用繼承自父類MyRectangle的width和height方法。
當(dāng)然,也支持覆蓋(或重寫)父類的同名方法。例子如下:
?1. 代碼:
? //1. MyRectangle.h
#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" );
? ? //調(diào)用繼承自父類的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. 編譯運(yùn)行 :
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
? ?(1) Square繼承自MyRectangle,重寫了setWidth和setHeight方法;
? ?(2) Square可以調(diào)用繼承自父類MyRectangle的width和height方法。
總結(jié)
以上是生活随笔為你收集整理的object-c的继承的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 利用计算机软件优化设计畜禽饲料配方 ex
- 下一篇: 内部推荐岗位信息201508