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指定的方法

测试实验设计

继承关系图:

wKiom1mzqYSScHWGAALtmGyk2ko626.png-wh_50

Rectangle.h

wKioL1mzqZazT3-7AACeTpj9ymk325.png-wh_50

Square.h

wKioL1mzqbWzTRC7AAB3OHz9RMc871.png-wh_50

在main.h中的测试如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
//
//  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;
}
}


wKioL1mzrJiieyITAAF_vxRqdwo625.png-wh_50















本文转自Aonaufly51CTO博客,原文链接:http://blog.51cto.com/aonaufly/1963970 ,如需转载请自行联系原作者



相关文章
|
C# iOS开发 Java
****Objective-C 中的方法的调用
oc语言中采用特定的语言调用类或者实例(对象)的方法称为发送消息或者方法调用。 oc中方法的调用有两种:  第一种: [类名或对象名 方法名];   [ClassOrInstance method]; [ClassOrInstance method:arg1]; ...
1024 0
|
iOS开发
objective-c 方法加号(+) 减号(-)
至于你说的用加号声明一个成员变量完全就是错误的理解。要把成员变量声明为静态的,必须使用static关键字。 本文转自博客园知识天地的博客,原文链接:objective-c 方法加号(+) 减号(-),如需转载请自行联系原博主。
2523 0
|
Java iOS开发
Objective-C方法与函数的区别
Objective-C方法与函数的区别 方法是唯对象所有 函数是不依赖于对象存在的 方法 函数 - (void)test; void test(); 方法是以减号 ...
955 0
|
JavaScript 前端开发 iOS开发
Javascript与Objective-C 字符串与数组的方法类比
table {border-collapse:collapse;} table td {border:1px solid #ccc;} String vs NSString JavaScript string ...
656 0
|
iOS开发
Objective-C中的NSObject对象经常使用到的方法
<span style="font-family:'Courier New'; margin:0px; padding:0px; line-height:1.5; color:rgb(0,128,0)">/*</span><span style="font-family:'Courier New'; margin:0px; padding:0px; line-height:1.5; col
904 0
|
Android开发 iOS开发
利用Objective-C运行时hook函数的三种方法
方法一,hook已有公开头文件的类: 首先写一个Utility函数: #import void exchangeMethod(Class aClass, SEL oldSEL, SEL newSEL) { Method oldMethod ...
1199 0
|
iOS开发
Objective-C的方法替换
Objective-C的方法替换 (Method Replacement for Fun and Profit) 本文将要讨论Objective-C中的方法替换(method replacement)和swizzling(移魂大法)。
875 0
|
缓存 iOS开发
Objective-c方法调用流程
Objective-c方法调用流程   Objective-c是一门动态语言,动态两个字主要就体现在我们调用方法的时候,运行时回动态的查找方法,然后调用相应的函数地址。运行时是整个Objective-c程序的基石,有了它我们的程序才能正常运行起来。
966 0