简体   繁体   English

AS3超级班刊

[英]AS3 Super Class Issue

I have a problem and I'm not too sure the best way to resolve it. 我有问题,但我不太确定解决问题的最佳方法。

Scenario: 场景:

I have a Super Class called 'food' and I have 20 different foods and extends 'food' like Pizza, Curry, Fish and Chip etc. 我有一个超级班级,称为“食品”,我有20种不同的食品,并扩展了“食品”,例如比萨,咖喱,鱼和薯条等。

When I remove a 'food' I keep a record of it so I can reuse (for performance purposes). 当我删除“食物”时,我会记录下来以便重新使用(出于性能目的)。 Can I make a new Pizza class that uses an old 'food'? 我可以使用旧的“食物”制作新的披萨类吗?

Eg 例如

public class Pizza extends food
{
      public function Pizza()
      {
           super = FOOD.returnUsedFoodClass();
      }
}

Is this possible or would I need to save the extending Class as well? 这是否可能,或者我还需要保存扩展的Class?

Hope this all make sense. 希望这一切都有道理。

EDIT: 编辑:

When I say remove I mean I no longer need it - so I would normally remove all references to it. 当我说删除时,我的意思是我不再需要它,因此我通常会删除所有对其的引用。 But instead, I have placed all 'food' classes that I no longer need in a static vector so I can reuse them later. 但是,相反,我将不再需要的所有“食物”类放置在静态向量中,以便以后再使用。

You misunderstand the basic OOP principles here. 您在这里误解了基本的OOP原则。

First: a constructor runs only once and only when the object is created so any attempt to stop the creation of the object or replace the object from within its constructor is illogical and cannot succeed since the object at that moment is already created. 首先:构造函数仅在对象创建后才运行一次,因此任何试图停止在其构造函数内创建对象或替换该对象的尝试都是不合逻辑的,并且由于该对象已被创建而无法成功。

Second: Classic misunderstanding of the super keyword. 第二:对super关键字的经典误解。 No super doesn't point to any other instance of any other object, super in constructor points to the super class implementation of the constructor. 没有super不会指向任何其他对象的任何其他实例,构造函数中的super指向构造函数的超类实现。 Trying to assign an object to super cannot work and is also illogical. 尝试将对象分配给super是行不通的,而且也不合逻辑。 I'm guessing you meant to use 'this' which would also not work anyway. 我猜您打算使用“ this”,但无论如何也无法使用。

What you are trying to achieve cannot be done that way and this in any OOP language. 您试图达到的目标无法用任何OOP语言来完成。 There's no way to run a constructor (meaning creating the object) and make this object point to something else within its own constructor. 无法运行构造函数(即创建对象)并使该对象指向其自己的构造函数中的其他对象。 What you are looking for is a classic object pooling system via static methods like this: 您正在寻找的是通过静态方法的经典对象池系统,如下所示:

var pizza:Pizza = Food.getFood("pizza") as Pizza;

Where the static method checks if any Pizza instance (from the pool) is available and if it is it returns it and if it's not it creates a new one and returns it. 静态方法在哪里检查(从池中)是否有任何Pizza实例可用,是否返回该比萨实例,如果不是,它将创建一个新实例并将其返回。

Pooling can be implemented loosely or explicitly, I prefer the more solid and flexible explicit version. 池可以松散地或显式地实现,我更喜欢更可靠和灵活的显式版本。 Here's an example: 这是一个例子:

Food class pooling additions: 食品类集合添加:

static private var recycledInstances:Vector.<Food> = new Vector.<Food>();
//hold our reclycled instances

public function recycle():void
{
    var index:int = recycledInstances.indexOf(this);
    if(index >= 0)
    {
        return;
    }
    recycledInstances.push(this);
}
//explicitly add this instance to recycle

private function reset():void
{

}
//to run in constructor and when the instance is retreived from recycle
//this method purpose is to reset all values to default.

Now when an instance is no longer used you call the instance recycle() method to place it in recycle. 现在,当不再使用实例时,您可以调用实例recycle()方法将其放入回收站。 Then when you need a new instance you do: 然后,当您需要一个新实例时,您可以执行以下操作:

var food:Food = Food.getFood();

And this is implemented that way in Food class: 这在食品课上是这样实现的:

static public function getFood():Food
{
    if(recycledInstances.length)
    {
         var totalInstances:uint = recycledInstances.length;
         var instance:Food = recycledInstances[totalInstances - 1];
         instance.reset();
         recycledInstances.length -= 1;//faster than splice
         return instance;
    }
    return new Food();
}

You can extend this easily to descendant of food class by adding a type member variable to Food for example and check the type of recycled instances before returning them. 您可以轻松地将其扩展到食品类的后代,例如,向食品添加类型成员变量,并在返回实例之前检查回收实例的类型。

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

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