日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

object-c的继承

發(fā)布時(shí)間:2023/12/14 编程问答 33 豆豆
生活随笔 收集整理的這篇文章主要介紹了 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

Square after width: 20Square after height: 20


3. 說明:
? ?(1) Square繼承自MyRectangle,重寫了setWidth和setHeight方法;
? ?(2) Square可以調(diào)用繼承自父類MyRectangle的width和height方法。

總結(jié)

以上是生活随笔為你收集整理的object-c的继承的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。