A.2.2 类型强制转换

2013-10-07 14:31:32 · 作者: · 浏览: 60

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;