Objective-C 处理动态类型的方法

方法 问题或行为
-(BOOL) isKindOfClass : class-object 对象是不是class-object或其子类的成员
-(BOOL) isMemberOfClass:class-object 对象是不是class-object的成员
-(BOOL) respondsToSelector:Selector 对象是否能够响应selector所指定的方法
-(BOOL) instancesRespondToSelector 指定的类实例是否能响应selector
-(BOOL) isSubclassOfClass:class-object 对象是否是指定类的子类
-(id) performSelector:selector 应用selector指定的方法
-(id) performSelector:selector withObject:object 应用selector指定的方法
-(id) performSelector:selector withObject:object1 withObject : object2 应用selector指定的方法

测试实验设计

继承关系图:

Objective-C 处理动态类型的方法

Rectangle.h

Objective-C 处理动态类型的方法

Square.h

Objective-C 处理动态类型的方法

在main.h中的测试如下:

//
//  main.m
//  Square
//
//  Created by Apple on 2017/9/9.
//  Copyright  2017年 Apple. All rights reserved.
//

#import <Foundation/Foundation.h>
#import "Square.h"



int main(int argc, const char * argv[]) {
    @autoreleasepool {
        Square *mySquare = [[Square alloc] init];
        
        //isMemberof
        if( [mySquare isMemberOfClass:[Square class]] == YES )
        {
            NSLog(@" mySquare is a member of Square class ");//
        }
        
        if( [mySquare isMemberOfClass:[Rectangle class]] == YES )
        {
            NSLog(@"mySquare is s member of Rectamgle class");
        }
        
        if([mySquare isMemberOfClass:[NSObject class]] == YES)
        {
            NSLog(@"mySquare is a member of NSObject class");
        }
        
        //isKindOf
        if( [mySquare isKindOfClass:[Square class]] == YES )
        {
            NSLog(@" mySquare is a kind of Square class ");//
        }
        
        if( [mySquare isKindOfClass:[Rectangle class]] == YES )
        {
            NSLog(@"mySquare is s kind of Rectamgle class");
        }
        
        if([mySquare isKindOfClass:[NSObject class]] == YES)
        {
            NSLog(@"mySquare is a kind of NSObject class");//
        }
        
        //respondsTo:
        if( [mySquare respondsToSelector:@selector(setSide:)] == YES )
        {
            NSLog(@" mySquare responds to setSide : method ");
        }
        
        if( [mySquare respondsToSelector:@selector(setWidth:addHeight:)] == YES )
        {
            NSLog(@"mySquare responds to setWidth:addHeight : method");
        }
        
        if([Square respondsToSelector:@selector(alloc)] == YES)
        {
            NSLog(@"Square class responds to alloc method");
        }
        
        
        //instancesRespondTo:
        if( [Rectangle instancesRespondToSelector:@selector(setSide:)] == YES )
        {
            NSLog(@"Instances of respond to setSide : method");
        }
        
        if( [Square instancesRespondToSelector:@selector(setWidth:addHeight:)] == YES )
        {
            NSLog(@"Instances of Square respond to setWidth:addHeight: : method");
        }
        
        if([Square isSubclassOfClass:[Rectangle class]] == YES)
        {
            NSLog(@"Square is a subclass of a rectangle");
        }
    return 0;
}
}


Objective-C 处理动态类型的方法