A.2.2 类型强制转换
类型转换指的是将某种数据类型的对象实例转换成另一种兼容的数据类型。例如,如果将方法输入参数的类型指定为该方法内的接口,那么可以将该对象转换成其原始类型。
ActionScript支持两种不同的类型转换。第一种使用as关键字,而第二种则是使用一种构造函数风格的语法,如下所示:
- //Method one:Using the as keyword
- Var tempVar: SomeVarType=inputParam as SomeVarType;
- //Method two:Using constructor-stype syntax
- Var tempVar:SomeVarTypeSomeVarType=SomeVarType(inputParam)
Java支持从一个子类型向上强制转换成一个父类型,以及向下强制将一个父类型转换成一个子类型: - //Method one:Upcasting from a subclass type to a parent class type
- SomeParentType tempParent=someChild;
- //Method two:Downcasting from a parent type to a child type
- SomeChildType anotherChild=(SomeChildType)tempParent;
C++(www.cppentry.com)类型强制转换有所不同。这里有个示例:- someParent=(SomeParentType)someChild;