继承 子类 可以继承父类的方法,从而实现子类可以利用父类的方法, 重写 是 子类可以重写父类的方法,这样子类的这个方法将覆盖掉父类的方法。 虚方法 使用时 可以通过父类的指针指向子类对象,从而子类对象到父类的引用。父类的实现
#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