设为首页 加入收藏

TOP

A.2.2 类型强制转换
2013-10-07 14:31:32 来源: 作者: 【 】 浏览:59
Tags:A.2.2 类型 强制 转换

A.2.2  类型强制转换

类型转换指的是将某种数据类型的对象实例转换成另一种兼容的数据类型。例如,如果将方法输入参数的类型指定为该方法内的接口,那么可以将该对象转换成其原始类型。

ActionScript支持两种不同的类型转换。第一种使用as关键字,而第二种则是使用一种构造函数风格的语法,如下所示:

  1. //Method one:Using the as keyword  
  2. Var tempVar: SomeVarType=inputParam as SomeVarType;  
  3. //Method two:Using constructor-stype syntax  
  4. Var tempVar:SomeVarTypeSomeVarType=SomeVarType(inputParam)  

Java支持从一个子类型向上强制转换成一个父类型,以及向下强制将一个父类型转换成一个子类型:
  1. //Method one:Upcasting from a subclass type to a parent class type  
  2. SomeParentType tempParent=someChild;  
  3. //Method two:Downcasting from a parent type to a child type  
  4. SomeChildType anotherChild=(SomeChildType)tempParent;  

C++(www.cppentry.com)类型强制转换有所不同。这里有个示例:
  1. someParent=(SomeParentType)someChild; 


】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
分享到: 
上一篇A.3.2 多态性 下一篇A.1.2 运行时引擎

评论

帐  号: 密码: (新用户注册)
验 证 码:
表  情:
内  容: