黑马程序员--类的继承

来源:互联网 发布:国泰安数据库是什么 编辑:程序博客网 时间:2024/05/16 04:35

<span style="font-family: Menlo;">/</span>
//  main.m//  简单的继承////  Created by smartlei on 15/5/20.//  Copyright (c) 2015年 smartlei. All rights reserved.//#import <Foundation/Foundation.h>/*************ClassA*****************/@interface ClassA :NSObject{    int x;}-(void) initVar;@end@implementation ClassA-(void) initVar{    x=100;}@end/****************ClassB*******************/@interface ClassB :ClassA//继承类A{    int y;}-(void) printfVar;@end@implementation ClassB-(void) printfVar{    NSLog(@"x=%i",x);}@endint main(int argc, const char * argv[]) {    @autoreleasepool {                ClassB *b=[[ClassB alloc] init];//申请空间        [b initVar];//对b初始化,但是b没有定义初始化函数,所以向下调用类A的初始化函数        [b printfVar];//调用b的打印函数        // insert code here...        //  NSLog(@"Hello, World!");    }    return 0;}

注意点一:如何找到正确的方法?

首先调用方法是首先会查看自己是否定义了该方法,如果定义了则调用该方法,如果没有定义则查找父类是否定义,如果定义了则调用父类该方法,一直重复循环直至查找到根类也没有则停止。















0 0
原创粉丝点击