当前位置: 首页 > 工具软件 > super-rentals > 使用案例 >

self and super

司空均
2023-12-01

Just explain it with Code:

//  A.h
#import <Foundation/Foundation.h>
@interface A : NSObject

- (void)test;
@end

//  A.m
#import "A.h"
@implementation A

- (void)test
{
    NSLog(@"A test");
    [self go];
}

- (void)go
{
    NSLog(@"A go");
}
@end

//  B.h
#import "A.h"
@interface B : A

- (void)test;
@end

//  B.m
#import "B.h"
@implementation B
- (void)test
{
    NSLog(@"B test");
    [super test];
    [self go];
}

- (void)go
{
    NSLog(@"B go");
}
@end

//  C.h
#import "B.h"
@interface C : B

- (void)test;
@end

//  C.m
#import "C.h"
@implementation C

- (void)test
{
    NSLog(@"C test");
    [super test];
    [self go];
}

- (void)go
{
    NSLog(@"C go");
}

@end

//  main.m
#import <Foundation/Foundation.h>
#import "C.h"

int main(int argc, const char * argv[])
{
    @autoreleasepool {
        C *c = [[C alloc] init];
        [c test];
        [c release];
        
        /** output:
            C test
            B test
            A test
            C go
            C go
            C go
         */
    }
    return 0;
}


 类似资料:

相关阅读

相关文章

相关问答