actionscript-3 – AS3将一种类型转换为另一种

前端之家收集整理的这篇文章主要介绍了actionscript-3 – AS3将一种类型转换为另一种前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我有一个名为Room的基类和一个名为Attic的子类,另一个名为Basement.

我有一个控制器类,它有一个名为CurrentLocation的属性,类型为Room.这个想法是我希望能够将Attic或Basement放在该属性中并将其重新放回来,然后将其转换为任何类型.

所以如果在控制器上的内容是Attic类型,我试图找出如何明确地转换它.我以为我知道但它不工作…这是我以为会是借用Java

var myAttic:Attic = (Attic) Controller.CurrentLocation;

这给我一个语法错误

1086: Syntax error: expecting semicolon before instance.

那么你如何隐含地投下呢?还是可以吗我可以发誓我以前做过3.

解决方法

以下是您在ActionScript 3中投射的选项:

>使用as.

var myAttic:Attic = Controller.CurrentLocation as Attic; // Assignment.
(Controller.CurrentLocation as Attic).propertyOrMethod(); // In-line use.

如果转换失败,这将为myAttic分配null.
> Wrap in Type().

var myAttic:Attic = Attic(Controller.CurrentLocation); // Assignment.
Attic(Controller.CurrentLocation).propertyOrMethod(); // In-line use.

如果演员失败,则会抛出TypeError.

猜你在找的Flash相关文章