isMemberOf,isKindOf,respondsTo,instancesRespondTo四种比较

//

//  main.m

//  ch9b

//

//  Created by ALEXUS WANG on 5/31/20.

//  Copyright © 2020 ClassroomM. All rights reserved.

//

 

#import

#import "Square.h"

 

int main(int argc, const char * argv[]) {

    @autoreleasepool {

        // insert code here...

        Square *mySquare = [[Square alloc] init];

        

        //isMemberOf

        if([mySquare isMemberOfClass:[Square class]] == YES)

            NSLog(@"mySquare is member of Suqare calss");

            

        if([mySquare isMemberOfClass:[Rectangle class]] == YES)

        NSLog(@"mySquare is member of Rectangle calss");

        

        if([mySquare isMemberOfClass:[NSObject class]] == YES)

        NSLog(@"mySquare is member of NSObject calss");

        

        //isKindOf

        if([mySquare isKindOfClass:[Square class]] == YES)

            NSLog(@"mySquare is kind of Suqare calss");

            

        if([mySquare isKindOfClass:[Rectangle class]] == YES)

        NSLog(@"mySquare is kind of Rectangle calss");

        

        if([mySquare isKindOfClass:[NSObject class]] == YES)

        NSLog(@"mySquare is kind of NSObject calss");

        

        //respondsTo

        if([mySquare respondsToSelector:@selector(setSide:)] == YES)

            NSLog(@"mySquare responds to setSide : method");

        

        if([mySquare respondsToSelector:@selector(setWidth:andHeight::)] == YES)

        NSLog(@"mySquare responds to setWidth:andHeight : method");

        

        if([mySquare respondsToSelector:@selector(alloc)] == YES)

        NSLog(@"mySquare responds to alloc : method");

        

        //instancesResopndTo

        if([Rectangle instancesRespondToSelector:@selector(setSide:)] == YES)

            NSLog(@"INstances of Rectangle respond to setSide:method");

        

        if([Square instancesRespondToSelector:@selector(setSide:)] == YES)

        NSLog(@"INstances of Square respond to setSide:method");

        

        if([Square isSubclassOfClass:[Rectangle class]] == YES)

            NSLog(@"Square is a subclass of rectangle");

    }

    return 0;

}

你可能感兴趣的:(Objective-C)