繼承 子類 可以繼承父類的方法,從而實現子類可以利用父類的方法, 重寫 是 子類可以重寫父類的方法,這樣子類的這個方法將覆蓋掉父類的方法。 虛方法 使用時 可以通過父類的指針指向子類對象,從而子類對象到父類的引用。父類的實現
#import<Foundation/Foundation.h>
@interfacefather :NSObject
{
@private
inta;
@protected
intb;
@public
intc;
int_age;
}
- (id) init;
- (void) setA:(int)newa;
- (int) getA;
- (void) setage:(int)newage;
- (void) printage;
- (void) jump; //定義一個虛方法,在子類中進行實現。
@end
#import"father.h"
@implementation father
- (id) init
{
if (self = [super init])
{
b = 4;
c = 5;
}
return self;
}
- (void) setA:(int)newa //這兩個就可以作為私有變量的橋梁了
{
a = newa;
}
- (int) getA
{
return a;
}
- (void) setage:(int)newage
{
_age = newage;
}
- (void) printage
{
NSLog(@"the age is %d", _age);
}
- (void) jump
{
return;
}
- (NSString *) description //后續在子類的方法中會有該方法的重寫
{
NSString *descrip = [[NSStringalloc] initWithFormat:@"the valuable is a = %d, b = %d, c = %d", a, b, c];
return ([descrip autorelease]);
}
@end
子類繼承的實現
#import<Foundation/Foundation.h>
#import"father.h"
@interfaceson :father
{
intd;
inte;
}
- (id) init;
@end
#import"son.h"
@implementation son
- (id) init
{
if (self = [super init])
{
d = 49;
e = 59;
}
return self;
}
- (void) jump
{
NSLog(@"the son is jump tall");
return;
}
- (NSString *) description //子類的重寫,這樣就會將父類的方法進行覆蓋
{
// [self description];
int newa = [super getA]; //這里應該用父類的一個調用,而不應該僅僅是getA
NSString * str = [[NSStringalloc] initWithFormat:@"a = %d, b = %d, c = %d, d = %d, e = %d", newa, b, c, d, e];
return ([str autorelease]);
}
@end
#import"father.h"
@interface doughter : father
@end
#import"doughter.h"
@implementation doughter
- (void) jump
{
NSLog(@"the doughter is jump down");
return;
}
@end
主程序的實現:這里jump即為虛方法:
#import<Foundation/Foundation.h>
#import"father.h"
#import"son.h"
#import"doughter.h"
intmain(intargc,constchar* argv[])
{
@autoreleasepool{
// insert code here...
NSLog(@"Hello, World!");
father*fat = [[fatheralloc]init];
NSLog(@"%@", fat);
[fatsetA:6];
NSLog(@"%@", fat);
son*so = [[sonalloc]init];
NSLog(@"the son is %@", so);
[sosetA:10]; //子類繼承了父類的setA方法,直接進行使用
NSLog(@"the father is %@", fat);
NSLog(@"the newson is %@", so); //這里用到了description方法,對father父類方法進行了重寫覆蓋
[sosetage:13];
[soprintage]; //子類繼承了父類的setA方法,直接進行使用
//虛方法的使用
//調用方法時不看指針看對象,
//由于父類的指針可以指向子類的對象,也即子類對象指向父類引用
father*fatson = [[sonalloc]init];
[fatsonjump];
father*fatdoughter = [[doughteralloc]init];
[fatdoughterjump];
[fatrelease];
[sorelease];
[fatsonrelease];
[fatdoughterrelease];
}
return0;
}
運行結果:
2014-03-20 22:12:20.480集成[1270:403] Hello, World!
2014-03-20 22:12:20.484集成[1270:403] the valuable is a = 0, b = 4, c = 5
2014-03-20 22:12:20.486集成[1270:403] the valuable is a = 6, b = 4, c = 5
2014-03-20 22:12:20.486集成[1270:403] the son is a = 0, b = 4, c = 5, d = 49, e = 59
2014-03-20 22:12:20.487集成[1270:403] the father is the valuable is a = 6, b = 4, c = 5
2014-03-20 22:12:20.488集成[1270:403] the newson is a = 10, b = 4, c = 5, d = 49, e = 59
2014-03-20 22:12:20.489集成[1270:403] the age is 13
2014-03-20 22:12:20.489集成[1270:403] the son is jump tall
2014-03-20 22:12:20.490集成[1270:403] the doughter is jump down