作为面向对象的编程语言,object-c当然也支持继承,而且和java一样,支持的是单一继承,即只有一个超类。
当然,也支持覆盖(或重写)父类的同名方法。例子如下:
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" );
//调用继承自父类的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
(1) Square继承自MyRectangle,重写了setWidth和setHeight方法;
(2) Square可以调用继承自父类MyRectangle的width和height方法。
当然,也支持覆盖(或重写)父类的同名方法。例子如下:
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" );
//调用继承自父类的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
(1) Square继承自MyRectangle,重写了setWidth和setHeight方法;
(2) Square可以调用继承自父类MyRectangle的width和height方法。