温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

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

发布时间:2020-07-07 20:06:51 来源:网络 阅读:788 作者:Aonaufly 栏目:开发技术

方法问题或行为
-(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 处理动态类型的方法

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI