對象在運行時獲取其類型的能力稱為內省。內省可以有多種方法實現。
判斷對象類型
-(BOOL) isKindOfClass: classObj判斷是否是這個類或者這個類的子類的實例
-(BOOL) isMemberOfClass: classObj 判斷是否是這個類的實例
我們試試這兩個方法的使用。
1、新建Person類繼承NSObject,新建Teacher類繼承Person
1.1、新建Person類
- #import <Foundation/Foundation.h>
-
- @interface Person : NSObject
- {
- NSString *name;
- }
- -(void)setName:(NSString*)n;
-
- @end
- #import "Person.h"
-
- @implementation Person
- -(void)setName:(NSString *)n
- {
- name = n;
- }
-
- @end
1.2新建Teacher類
- #import "Person.h"
-
- @interface Teacher : Person
-
- -(void)teach;
-
- @end
- #import "Teacher.h"
-
- @implementation Teacher
- -(void)teach
- {
- NSLog(@"我教數學");
- }
- @end
1.3 我們先實驗下isMemberOfClass方法。
- 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方法
- 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的使用
這裡不寫對象的創建和釋放了,參考上面的代碼
- // 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
- // 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
3、Objective-C的id類型
C++ 使用的是強類型:對象必須符合其類型,否則不能通過編譯。在 Objective-C 中,id類型類似於(void*) ,可以指向任何類的實例。而不需要強制轉換。
下面看看使用,
先把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] 張三老師 教數學