方法 | 问题或行为 |
-(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指定的方法 |
测试实验设计
继承关系图:
Rectangle.h
Square.h
在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;
}
}
|
本文转自Aonaufly51CTO博客,原文链接:http://blog.51cto.com/aonaufly/1963970 ,如需转载请自行联系原作者