对象在运行时获取其类型的能力称为内省。内省可以有多种方法实现。
判断对象类型
-(BOOL) isKindOfClass: classObj判断是否是这个类或者这个类的子类的实例
-(BOOL) isMemberOfClass: classObj 判断是否是这个类的实例
我们试试这两个方法的使用。
1、新建Person类继承NSObject,新建Teacher类继承Person
1.1、新建Person类
1.#import <Foundation/Foundation.h>
2.
3.@interface Person : NSObject
4.{
5. NSString *name;
6.}
7.-(void)setName:(NSString*)n; 8.
9.@end
1.#import "Person.h"
2.
3.@implementation Person
4.-(void)setName:(NSString *)n 5.{
6. name = n;
7.}
8.
9.@end
1.2新建Teacher类
1.#import "Person.h"
2.
3.@interface Teacher : Person
4.
5.-(void)teach; 6.
7.@end
1.#import "Teacher.h"
2.
3.@implementation Teacher
4.-(void)teach 5.{
6. NSLog(@"我教数学"); 7.}
8.@end
1.3 我们先实验下isMemberOfClass方法。
1.NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
2. Person *person = [[Person alloc] init];
3. Teacher *teacher = [[Teacher alloc] init];
4.
5. //YES 6. if ([teacher isMemberOfClass:[Teacher class]]) { 7. NSLog(@"teacher Teacher类的成员"); 8. }
9. //NO 10. if ([teacher isMemberOfClass:[Person class]]) { 11. NSLog(@"teacher Person类的成员"); 12. }
13. //NO 14. if ([teacher isMemberOfClass:[NSObject class]]) { 15. NSLog(@"teacher NSObject类的成员"); 16. }
17. [person release];
18. [teacher release];
19. [pool release];
打印结果:
2012-07-04 14:23:07.965 ObjectiveCTest[2460:f803] teacher Teacher类的成员
只有第一个判断打印出来,isMemberOfClass判断是否是属于这类的实例,是否跟父类有关系他不管。
1.4 isMemberOfClass方法
1.NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
2.Person *person = [[Person alloc] init];
3.Teacher *teacher = [[Teacher alloc] init];
4.
5.//YES 6.if ([teacher isKindOfClass:[Teacher class]]) { 7. NSLog(@"teacher 是 Teacher类或Teacher的子类"); 8.}
9.//YES 10.if ([teacher isKindOfClass:[Person class]]) { 11. NSLog(@"teacher 是 Person类或Person的子类"); 12.}
13.//YES 14.if ([teacher isKindOfClass:[NSObject class]]) { 15. NSLog(@"teacher 是 NSObject类或NSObject的子类"); 16.}
17.[person release];
18.[teacher release];
19.[pool release];
2012-07-04 14:34:17.315 ObjectiveCTest[2595:f803] teacher 是 Teacher类或Teacher的子类
2012-07-04 14:34:17.316 ObjectiveCTest[2595:f803] teacher 是 Person类或Person的子类
2012-07-04 14:34:17.316 ObjectiveCTest[2595:f803] teacher 是 NSObject类或NSObject的子类
三个结果都打印出来了。
2、
-(BOOL) respondsToSelector: selector 判读实例是否有这样方法
+(BOOL) instancesRespondToSelector: 判断类是否有这个方法。此方法是类方法,不能用在类的对象
2.1 respondsToSelector的使用
这里不写对象的创建和释放了,参考上面的代码
1.// YES
2.if ( [teacher respondsToSelector: @selector( setName: )] == YES ) { 3. NSLog(@"teacher responds to setSize: method" ); 4.}
5.
6.// NO 7.if ( [teacher respondsToSelector: @selector( abcde )] == YES ) { 8. NSLog(@"teacher responds to nonExistant method" ); 9.}
10.
11.// YES 12.if ( [Teacher respondsToSelector: @selector( alloc )] == YES ) { 13. NSLog(@"teacher class responds to alloc method\n" ); 14.}
打印结果:
2012-07-04 14:39:49.853 ObjectiveCTest[2723:f803] teacher responds to setSize: method
2012-07-04 14:39:49.854 ObjectiveCTest[2723:f803] teacher class responds to alloc method
中间的那个判断我随便写了个selector,当然没有了。respondsToSelector 检查类方法 alloc返回YES
2.2 instancesRespondToSelector
1.// NO
2. if ( [Person instancesRespondToSelector: @selector(teach)] == YES ) { 3. NSLog(@"Person instance responds to teach method" ); 4. }
5.
6. // YES 7. if ( [Teacher instancesRespondToSelector: @selector(teach)] == YES ) { 8. NSLog(@"Teacher instance responds to teach method"); 9. }
10. // YES 11. if ( [Teacher instancesRespondToSelector: @selector(setName:)] == YES ) { 12. NSLog(@"Teacher instance responds to setName: method" ); 13. }
打印结果:
1.2012-07-04 14:52:29.378 ObjectiveCTest[2961:f803] Teacher instance responds to teach method
2.2012-07-04 14:52:29.379 ObjectiveCTest[2961:f803] Teacher instance responds to setName: method
3、Objective-C的id类型
C++ 使用的是强类型:对象必须符合其类型,否则不能通过编译。在 Objective-C 中,id类型类似于(void*) ,可以指向任何类的实例。而不需要强制转换。
下面看看使用,
先把Teacher类中的 teach方法修改一下,改成
-(void)teach
{
NSLog(@"%@ 教数学" ,name);
}
然后实现并调用
1.NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
2.Person *person = [[Person alloc] init];
3.Teacher *teacher = [[Teacher alloc] init];
4.
5.id p = person;
6.id t = teacher;
7.[t setName:@"张三老师"]; 8.[t teach];
9.
10.[person release];
11.[teacher release];
12.[pool release];
打印结果:
1.2012-07-04 14:57:55.905 ObjectiveCTest[3085:f803] 张三老师 教数学
本篇文章来源于 Linux公社网站(www.linuxidc.com) 原文链接:http://www.linuxidc.com/Linux/2012-07/64453.htm