当前位置:编程学习 > C/C++ >>

Objective-C语法之动态类型(isKindOfClass, isMemberOfClass,id)等

对象在运行时获取其类型的能力称为内省。内省可以有多种方法实现。
判断对象类型
-(BOOL) isKindOfClass: classObj判断是否是这个类或者这个类的子类的实例
-(BOOL) isMemberOfClass: classObj 判断是否是这个类的实例
我们试试这两个方法的使用。
1、新建Person类继承NSObject,新建Teacher类继承Person
1.1、新建Person类
[cpp] 
#import <Foundation/Foundation.h> 
 
@inte易做图ce Person : NSObject 

    NSString *name; 

-(void)setName:(NSString*)n; 
 
@end 

[cpp] 
#import "Person.h" 
 
@implementation Person 
-(void)setName:(NSString *)n 

    name = n; 

 
@end 
1.2新建Teacher类
[cpp] 
#import "Person.h" 
 
@inte易做图ce Teacher : Person 
 
-(void)teach; 
 
@end 
[cpp] 
#import "Teacher.h" 
 
@implementation Teacher 
-(void)teach 

    NSLog(@"我教数学"); 

@end 
1.3 我们先实验下isMemberOfClass方法。
[cpp]
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判断是否是属于这类的实例,是否跟父类有关系他不管。
1.4 isMemberOfClass方法
[cpp] 
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的子类
三个结果都打印出来了。
2、
-(BOOL) respondsToSelector: selector 判读实例是否有这样方法
+(BOOL) instancesRespondToSelector:  判断类是否有这个方法。此方法是类方法,不能用在类的对象
2.1 respondsToSelector的使用
这里不写对象的创建和释放了,参考上面的代码
[cpp] 
// 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
2.2 instancesRespondToSelector
[cpp] 
// 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" ); 
    } 
打印结果:
[cpp] 
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 

3、Objective-C的id类型

C++ 使用的是强类型:对象必须符合其类型,否则不能通过编译。在 Objective-C 中,id类型类似于(void*) ,可以指向任何类的实例。而不需要强制转换。
下面看看使用,
先把Teacher类中的 teach方法修改一下,改成
-(void)teach
{
    NSLog(@"%@ 教数学" ,name);
}
然后实现并调用
[cpp] 
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]; 
打印结果:
[cpp] view plaincopy
2012-07-04 14:57:55.905 ObjectiveCTest[3085:f803] 张三老师 教数学 


作者:totogo2010
补充:软件开发 , C语言 ,
CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,