继承
继承的语法
在类的头文件中使用 ClassName : SuperClassName 的形式完成继承。
我们每一个类都默认继承了NSObject 。
// 继承NSObject@interface Person : NSObject@end
子类不能访问父类的私有元素(实例变量、方法等)。
方法的复写
子类可以复写父类的方法,比如我们可以复写父类的description方法来展示当前类的信息。
当使用NSLog的时候,NSLog("%@",object) ; 会自动调用object 的 description方法。
@interface Person : NSObject-(NSString *) description ;@end@implementation Person-(NSString *) description{NSString * str = @"execute Person description ." ;return [str autorelease] ;}@end
Person *person = [[Person alloc] init];// 会调用Person 的 description方法。NSLog(@"person.description = %@" , person) ;[person release] ;

浙公网安备 33010602011771号