简体   繁体   English

AS3动态创建对象的实例

[英]AS3 Dynamically create an instance of an object

I have a class which contains the following method to place an object on the stage. 我有一个类,它包含以下方法将对象放在舞台上。

public function createBox()
{
    var box:Ball1 = new Ball1();
    addChild(box);
    box.addEventListener(Event.ENTER_FRAME, boxF);
}

What I would like to do is to is pass an object name to the method, and load that object instead, thus allowing me to use the same method for different objects. 我想要做的是将对象名称传递给方法,然后加载该对象,从而允许我对不同的对象使用相同的方法。

A non-working example: 一个非工作的例子:

public function createBox( obj )
{
    var box:obj = new obj();
    addChild(box);
    box.addEventListener(Event.ENTER_FRAME, boxF);
}

Is this possible? 这可能吗?

Thanks 谢谢

A more agile version of the existing answer is to use getDefinitionByName() , which allows you to construct a class based on an input string. 现有答案的更敏捷版本是使用getDefinitionByName() ,它允许您基于输入字符串构造类。

Using this function, you can rewrite the method to something like this: 使用此函数,您可以将方法重写为以下内容:

public function produce(className:String):*
{
    var type:Class = getDefinitionByName(className) as Class;
    return new type();
}


Advanced: 高级:

To make this stricter and more maintainable (make it so only certain factories can create certain classes), you can use an interface to build a relationship between a given factory and the classes it can produce. 为了使这更严格和更易于维护(使得只有某些工厂可以创建某些类),您可以使用接口在给定工厂和它可以生成的类之间建立关系。 A small example of this follows below. 下面是一个小例子。

Say a factory, EnemyFactory , creates objects that you would consider to be enemies in a game. 说一个工厂, EnemyFactory ,创建你认为是游戏中的敌人的物体。 We don't want to be able to create things like pickups, particles and other non-enemy type objects. 我们不希望能够创建拾取器,粒子和其他非敌人类型的对象。 We can create an interface IEnemyProduct which is implemented by classes that the EnemyFactory is allowed to create. 我们可以创建一个IEnemyProduct接口,它由EnemyFactory允许创建的类实现。 The interface could be as simple as: 界面可以简单如下:

public interface IEnemyProduct{}

Which would be implemented by any enemy classes. 任何敌人的类都可以实施。 The EnemyFactory 's produce() function can then be modified to a more readable version, like this: 然后可以将EnemyFactoryproduce()函数修改为更易读的版本,如下所示:

public function produce(enemyClassName:String):IEnemyProduct
{
    var type:Class = getDefinitionByName(enemyClassName) as Class;
    return new type() as IEnemyProduct;
}

In this case, produce() will return null if the produced class does not implement IEnemyProduct . 在这种情况下,如果生成的类没有实现IEnemyProduct ,则produce()将返回null The goal here is to make it obvious which factories are responsible for which objects, which is a big advantage once the project becomes larger. 这里的目标是明确哪些工厂负责哪些对象,一旦项目变大,这是一个很大的优势。 The use of an interface rather than a base class also means you can implement multiple interfaces and have the class produced by multiple factories. 使用接口而不是基类也意味着您可以实现多个接口并使多个工厂生成类。

You could maybe use a simple factory, or something similar: 你可以使用一个简单的工厂,或类似的东西:

  public class Factory
  {
      public static function produce(obj:String)
      {
          if (obj == "obj1")
          {
              return new obj1();
          } else if (obj == "obj2") {
              return new obj2();
          }
      }
  }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM