ObjC学习4-多态、动态类型、动态绑定及异常处理
2013-05-20 17:29
302 查看
1.动态绑定和ID类型
这里建了两个灰常简单的类等会用ID类型去实现它
@interface ClassA:NSObject
{
}
-(void) print;
@end
@implementation ClassA
-(void) print
{
NSLog(@"i am a!");
}
@end
@interface ClassB:NSObject
{
}
-(void) print;
@end
@implementation ClassB
-(void) print
{
NSLog(@"i am b!");
}
@end
主程序代码:
ClassA * a = [[ClassA alloc]init];
ClassB *b = [[ClassB alloc]init];
id * i ;//定义一个ID类型
i=a;
[i print];
i=b;
[i print];
[a release];
[b release];
结果:
![](http://img.blog.csdn.net/20130520154519343)
这里可看出ID 类型是一个灰常灵活的类型,结合多台,动态绑定和动态类型就可以轻易编写出来自不同类型的对象发送的相同消息的代码。
类似C#里面的Object类型~
*使用ID有一点需要注意:由于ID类型是动态的所以编译器只需要知道方法名及参数就好类型什么的好像是忽略了,所以有时代码编译时不会出错,但运行时就出错。所以在原则上可以尽量少使用ID类型。
2.使用不同类型的变量时,要是想知道这个变量的类型是否是某个类型子类?或是是否有某个方法?
在原代码基础上添加
if([a class]==[b class])//是否是相同类型
{
NSLog(@"yes");
}else{
NSLog(@"no");
}
if([i class]==[b class])
{
NSLog(@"yes");
}else{
NSLog(@"no");
}
if([a isMemberOf:[NSObject class]])//是否是NSO成员
{
NSLog(@"is member of nso");
}else{
NSLog(@"not memeber of nso");
}
if([a isMemberOf:[ClassA class]])
{
NSLog(@"is member of nso");
}else{
NSLog(@"not memeber of nso");
}
if([a isKindOf:[NSObject class]])//是否是子类或其子类成员
{
NSLog(@"is kind of nso");
}else{
NSLog(@"not kind of nso");
}
if([a isKindOf:[ClassA class]])
{
NSLog(@"is kind of A");
}else{
NSLog(@"not kind of A");
}
![](http://img.blog.csdn.net/20130520164526837)
//@selector方法可以用SEL类型去接收
if([i respondsToSelector:@selector(print)] ==YES) //判断是否有print方法,再执行
{
[i print];
}else{
NSLog(@"not fun print");
}
if([i respondsToSelector:@selector(ok)] ==YES)
{
[i ok];
}else{
NSLog(@"not fun ok");
}
NSLog(@"%@",[i respondsToSelector:@selector(haha)] ?@"YES":@"NO");
结果为 :
i am b !
not fun ok
NO
由此可以让ID类型更灵活 减少运行时错误.
3.异常处理
@try
{
[i ok];
}
@catch(NSException *e)
{
NSLog(@"Caught %@%@",[e name],[e reason]);//输出异常名称,异常内容
//@throw;抛出异常
}
和别的语言一样 有try 有catch 有 throw ,
只不过是以一种指令的形式表现出来@~~~
-------------------------------------------------------------------------
今天是520,是你在try,我在默默地catch,throw1326,爱乐老婆宝宝~
这里建了两个灰常简单的类等会用ID类型去实现它
@interface ClassA:NSObject
{
}
-(void) print;
@end
@implementation ClassA
-(void) print
{
NSLog(@"i am a!");
}
@end
@interface ClassB:NSObject
{
}
-(void) print;
@end
@implementation ClassB
-(void) print
{
NSLog(@"i am b!");
}
@end
主程序代码:
ClassA * a = [[ClassA alloc]init];
ClassB *b = [[ClassB alloc]init];
id * i ;//定义一个ID类型
i=a;
[i print];
i=b;
[i print];
[a release];
[b release];
结果:
这里可看出ID 类型是一个灰常灵活的类型,结合多台,动态绑定和动态类型就可以轻易编写出来自不同类型的对象发送的相同消息的代码。
类似C#里面的Object类型~
*使用ID有一点需要注意:由于ID类型是动态的所以编译器只需要知道方法名及参数就好类型什么的好像是忽略了,所以有时代码编译时不会出错,但运行时就出错。所以在原则上可以尽量少使用ID类型。
2.使用不同类型的变量时,要是想知道这个变量的类型是否是某个类型子类?或是是否有某个方法?
在原代码基础上添加
if([a class]==[b class])//是否是相同类型
{
NSLog(@"yes");
}else{
NSLog(@"no");
}
if([i class]==[b class])
{
NSLog(@"yes");
}else{
NSLog(@"no");
}
if([a isMemberOf:[NSObject class]])//是否是NSO成员
{
NSLog(@"is member of nso");
}else{
NSLog(@"not memeber of nso");
}
if([a isMemberOf:[ClassA class]])
{
NSLog(@"is member of nso");
}else{
NSLog(@"not memeber of nso");
}
if([a isKindOf:[NSObject class]])//是否是子类或其子类成员
{
NSLog(@"is kind of nso");
}else{
NSLog(@"not kind of nso");
}
if([a isKindOf:[ClassA class]])
{
NSLog(@"is kind of A");
}else{
NSLog(@"not kind of A");
}
//@selector方法可以用SEL类型去接收
if([i respondsToSelector:@selector(print)] ==YES) //判断是否有print方法,再执行
{
[i print];
}else{
NSLog(@"not fun print");
}
if([i respondsToSelector:@selector(ok)] ==YES)
{
[i ok];
}else{
NSLog(@"not fun ok");
}
NSLog(@"%@",[i respondsToSelector:@selector(haha)] ?@"YES":@"NO");
结果为 :
i am b !
not fun ok
NO
由此可以让ID类型更灵活 减少运行时错误.
3.异常处理
@try
{
[i ok];
}
@catch(NSException *e)
{
NSLog(@"Caught %@%@",[e name],[e reason]);//输出异常名称,异常内容
//@throw;抛出异常
}
和别的语言一样 有try 有catch 有 throw ,
只不过是以一种指令的形式表现出来@~~~
-------------------------------------------------------------------------
今天是520,是你在try,我在默默地catch,throw1326,爱乐老婆宝宝~
相关文章推荐
- [学习笔记—Objective-C]《Objective-C 程序设计 第6版》第九章 多态,动态类型和动态绑定
- Programming in Objective C学习笔记03——多态、动态绑定和动态类型
- 黑马程序员——Objective-C程序设计(第4版)学习笔记之09-多态、动态类型和动态绑定——黑马 IOS 技术博客
- obj-c编程05:类的多态与id动态绑定
- Python3学习笔记06-循环设计、循环对象、函数对象、错误处理、动态类型
- obj-c编程05:类的多态与id动态绑定
- python3.4学习笔记(二) 类型判断,异常处理,终止程序
- obj-c编程05:类的多态与id动态绑定
- IOS第八天——Obj-C的多态性、动态类型和动态绑定
- OC多态,动态类型绑定
- (七)类、超类和子类 ——(多态,动态绑定,final类,类型转换,抽象类)
- C++中的动态类型与动态绑定、虚函数、运行时多态的实现
- Objective-c 特性之多态、动态类型和动态绑定
- 多态、动态绑定和动态类型
- 多态和动态绑定、多态和异常
- 多态、动态类型和动态绑定
- OC基础6:多态、动态类型和动态绑定
- java学习之编译时类型(前期绑定)和运行时类型 (后期绑定、动态绑定、运行时绑定)
- struts2 常用的结果类型,动态结果,异常处理,如何传递参数与常用的通配符映射
- C++中的动态类型与动态绑定、虚函数、运行时多态的实现