AS3 Casting one type to another

前端 未结 1 1985
借酒劲吻你
借酒劲吻你 2020-12-18 23:10

I have a base class called Room and a subclass called Attic, and another called Basement.

I have a controller class that has a

1条回答
  •  佛祖请我去吃肉
    2020-12-18 23:26

    Here are your options for casting in ActionScript 3:

    1. Use as.

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

      This will assign null to myAttic if the cast fails.

    2. Wrap in Type().

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

      This throws a TypeError if the cast fails.

    0 讨论(0)
提交回复
热议问题