1、动态绑定 1)在OC中,一个对象是否调用指定的方法不是有编译器决定而是由运行时决定,这杯称作【方法的动态绑定】 2)在OC中,对象不调用方法,而是接受消息,消息表达式为:[reciver message];
2、动态类型检测方法 1)判断类型 A、- (BOOL)isKindOfClass:(Class)aClass; 判断某个对象是不是某个类的实例对象或者是其子类的实例对象 B、- (BOOL)isMemberOfClass:(Class)aClass; 判断对象是否是指定类的实例,不管是不是其子类的实例 C、+ (BOOL)isSubclassOfClass:(Class)aClass; //判断类是否是指定类的子类 例如: - #import <Foundation/Foundation.h>
- #pragma mark Animal类声明部分
- @interface Animal: NSObject
- -(void)eat;
- @end
- #pragma mark Animal类实现部分
- @implementation Animal
- -(void)eat{
- NSLog(@"Animal eat...");
- }
- @end
- #pragma mark Dog类声明部分
- @interface Dog: Animal
- -(void)eat;
- @end
- #pragma mark Dog类实现部分
- @implementation Dog
- -(void)eat{
- NSLog(@"Dog eat...");
- }
- @end
-
-
- int main(int argc, const char * argv[])
- {
-
- @autoreleasepool {
- id dog = [Dog new];
- //检测一下是否是Animal的类实例对象,或是其子类的实例
- if ([dog isKindOfClass:[Animal class]]) {
- NSLog(@"dog is a kind of Animal");
- } else{
- NSLog(@"dog is not a kind of Animal");
- }
- //判断对象是否是指定类的实例,不管是不是其子类的实例
- if ([dog isMemberOfClass:[Animal class]]) {
- NSLog(@"dog is a member of Animal");
- }else{
- NSLog(@"dog is not a member of Animal");
- }
- //判断类是否是指定类的子类
- //[Dog isSubclassOfClass:[Animal class]]也可以
- if ([[dog class]isSubclassOfClass:[Animal class]]) {
- NSLog(@"dog is a subclass of Animal");
- }else{
- NSLog(@"dog is not a subclass of Animal");
- }
-
- }
- return 0;
- }
复制代码
打印结果: 2015-10-05 11:11:57.473 Demo01[1600:303] dog is a kindof Animal 2015-10-05 11:11:57.474 Demo01[1600:303] dog is not amember of Animal 2015-10-05 11:11:57.475 Demo01[1600:303] dog is asubclass of Animal
2)判断某一个类实例变量能否响应某个方法 例如: - id dog = [Dog new];
- //判断dog能否响应eat方法
- if ([dog respondsToSelector:@selector(eat)]) {
- [dog eat];
- }else{
- NSLog(@"dog cannnot respond to eat function");
- }
复制代码
打印结果; 2015-10-05 11:15:34.217 Demo01[1620:303] Dog eat...
3)判断类是否有某个方法 - //判断类能否响应eat方法,可以理解为该类中有木有eat方法,可以是从父类继承的,【注意】更对象响应不一样
- if ([Dog instancesRespondToSelector:@selector(eat)]) {
- NSLog(@"Dog have eat function");
- }else{
- NSLog(@"Dog have no eat function");
- }
复制代码
打印结果:
2015-10-05 11:21:14.717 Demo01[1702:303] Dog have eatfunction
4)响应方法 - <font size="4">#import <Foundation/Foundation.h>
- #pragma mark Animal类声明部分
- @interface Animal: NSObject
- -(void)eat;
- @end
- #pragma mark Animal类实现部分
- @implementation Animal
- -(void)eat{
- NSLog(@"Animal eat...");
- }
- @end
- #pragma mark Dog类声明部分
- @interface Dog: Animal
- -(void)eat:(NSString*)food;
- -(void)eat:(NSString*)name andFood:(NSString*)food;
- @end
- #pragma mark Dog类实现部分
- @implementation Dog
- -(void)eat:(NSString*)food{
- NSLog(@"Dog eat %@",food);
- }
- -(void)eat:(NSString*)name andFood:(NSString*)food{
- NSLog(@"%@ eat %@",name,food);
- }
- @end
- int main(int argc, const char * argv[])
- {
- @autoreleasepool {
- id dog = [Dog new];
- //判断dog能否响应eat方法
- //响应无参方法
- SEL sel = @selector(eat);
- if ([dog respondsToSelector:sel]) {
- [dog performSelector:sel];
- }else{
- NSLog(@"dog cannnot respond to eat function");
- }
- //响应有参方法,一个参数
- SEL sel2 = @selector(eat:);
- if ([dog respondsToSelector:sel2]) {
- [dog performSelector:sel2 withObject:@"Fish"];
- }else{
- NSLog(@"dog cannnot respond to eat function");
- }
- //响应有参方法,两个参数
- SEL sel3 = @selector(eat:andFood:);
- if ([dog respondsToSelector:sel3]) {
- [dog performSelector:sel3 withObject:@"小黑" withObject:@"Shit"];
- }else{
- NSLog(@"dog cannnot respond to eat function");
- }
- }
- return 0;
- }</font>
复制代码打印结果: 2015-10-05 11:41:15.543 Demo01[1871:303] Animal eat... 2015-10-05 11:41:15.546 Demo01[1871:303] Dog eat Fish 2015-10-05 11:41:15.546 Demo01[1871:303] 小黑 eat Shit
|