对象在运行时获取其类型的能力称为内省。内省可以有多种方法实现。
判断对象类型
-(BOOL) isKindOfClass: classObj判断是否是这个类或者这个类的子类的实例
-(BOOL) isMemberOfClass: classObj 判断是否是这个类的实例
我们试试这两个方法的使用。
#import
@interface Person : NSObject
{
NSString *name;
}
-(void)setName:(NSString*)n;
@end
#import "Person.h"
@implementation Person
-(void)setName:(NSString *)n
{
name = n;
}
@end
#import "Person.h"
@interface Teacher : Person
-(void)teach;
@end
#import "Teacher.h"
@implementation Teacher
-(void)teach
{
NSLog(@"我教数学");
}
@end
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
Person *person = [[Person alloc] init];
Teacher *teacher = [[Teacher alloc] init];
//YES
if ([teacher isMemberOfClass:[Teacher class]]) {
NSLog(@"teacher Teacher类的成员");
}
//NO
if ([teacher isMemberOfClass:[Person class]]) {
NSLog(@"teacher Person类的成员");
}
//NO
if ([teacher isMemberOfClass:[NSObject class]]) {
NSLog(@"teacher NSObject类的成员");
}
[person release];
[teacher release];
[pool release];
打印结果:
2012-07-04 14:23:07.965 ObjectiveCTest[2460:f803] teacher Teacher类的成员
只有第一个判断打印出来,isMemberOfClass判断是否是属于这类的实例,是否跟父类有关系他不管。
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
Person *person = [[Person alloc] init];
Teacher *teacher = [[Teacher alloc] init];
//YES
if ([teacher isKindOfClass:[Teacher class]]) {
NSLog(@"teacher 是 Teacher类或Teacher的子类");
}
//YES
if ([teacher isKindOfClass:[Person class]]) {
NSLog(@"teacher 是 Person类或Person的子类");
}
//YES
if ([teacher isKindOfClass:[NSObject class]]) {
NSLog(@"teacher 是 NSObject类或NSObject的子类");
}
[person release];
[teacher release];
[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的子类
三个结果都打印出来了。+(BOOL) instancesRespondToSelector: 判断类是否有这个方法。此方法是类方法,不能用在类的对象
这里不写对象的创建和释放了,参考上面的代码
// YES
if ( [teacher respondsToSelector: @selector( setName: )] == YES ) {
NSLog(@"teacher responds to setSize: method" );
}
// NO
if ( [teacher respondsToSelector: @selector( abcde )] == YES ) {
NSLog(@"teacher responds to nonExistant method" );
}
// YES
if ( [Teacher respondsToSelector: @selector( alloc )] == YES ) {
NSLog(@"teacher class responds to alloc method\n" );
}
打印结果:
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
// NO
if ( [Person instancesRespondToSelector: @selector(teach)] == YES ) {
NSLog(@"Person instance responds to teach method" );
}
// YES
if ( [Teacher instancesRespondToSelector: @selector(teach)] == YES ) {
NSLog(@"Teacher instance responds to teach method");
}
// YES
if ( [Teacher instancesRespondToSelector: @selector(setName:)] == YES ) {
NSLog(@"Teacher instance responds to setName: method" );
}
打印结果:
2012-07-04 14:52:29.378 ObjectiveCTest[2961:f803] Teacher instance responds to teach method
2012-07-04 14:52:29.379 ObjectiveCTest[2961:f803] Teacher instance responds to setName: method
下面看看使用,
先把Teacher类中的 teach方法修改一下,改成
-(void)teach
{
NSLog(@"%@ 教数学" ,name);
}
然后实现并调用 NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
Person *person = [[Person alloc] init];
Teacher *teacher = [[Teacher alloc] init];
id p = person;
id t = teacher;
[t setName:@"张三老师"];
[t teach];
[person release];
[teacher release];
[pool release];
打印结果:
2012-07-04 14:57:55.905 ObjectiveCTest[3085:f803] 张三老师 教数学
著作权声明:本文由http://blog.csdn.net/totogo2010/原创,欢迎转载分享。请尊重作者劳动,转载时保留该声明和作者博客链接,谢谢!