创建类的新实例时堆栈溢出

时间:2014-09-07 12:25:09

标签: actionscript-3 stack-overflow

我是Action Script的新手,并且非常困惑我为什么会出现堆栈溢出。我将发布所有3个课程(相关内容)。我得到的输出。有人可以向我解释为什么这会导致堆栈溢出吗?

Main Class:

        public function Main() {
            //Problem starts here
            player1 = new Player();
            player2 = new Player();
// More code that does not matter
        }

玩家等级:

package  {

    import fl.motion.Color;
    import flash.display.MovieClip;

    public class Player extends MovieClip {

        public function Player() {
            var index:Finger = new Finger();
            var middle:Finger = new Finger();
            var ring:Finger = new Finger();
            var pinkie:Finger = new Finger();
        }
    }   
}

手指类:

package  {

    public class Finger extends Player {

        var colorOn:String;

        public function Finger(){
            colorOn = ""
        }

        function SetColor(colour:String):void {
            this.colorOn = colour;
        }
    }
}

输出:

Is this where stack overflow accuros?
Error: Error #1023: Stack overflow occurred.
    at flash.display::DisplayObject()
    at flash.display::InteractiveObject()
    at flash.display::DisplayObjectContainer()
    at flash.display::Sprite()
    at flash.display::MovieClip()
    at Player()
    at Finger()
    at Player()
    at Finger()
    at Player()
    at Finger()
    at Player()
    at Finger()
    at Player()
    at Finger()
    at Player()
    at Finger()
    at Player()
    at Finger()
    at Player()
    at Finger()
    at Player()
    at Finger()
    at Player()
    at Finger()
    at Player()
    at Finger()
    at Player()
    at Finger()
    at Player()
    at Finger()
    at Player()
    at Finger()
    at Player()
    at Finger()
    at Player()
    at Finger()
    at Player()
    at Finger()
    at Player()
    at Finger()
    at Player()
    at Finger()
    at Player()
    at Finger()
    at Player()
    at Finger()
    at Player()
    at Finger()
    at Player()
    at Finger()
    at Player()
    at Finger()
    at Player()
    at Finger()
    at Player()
    at Finger()
    at Player()
    at Finger()
    at Player()
    at Finger()
    at Player()
    at Finger()
    at Player()

1 个答案:

答案 0 :(得分:3)

要创建Player,您需要创建四个新Finger个对象...但每个Finger对象也是Player(因为Finger扩展Player)。因此,创建这四个Finger对象中的每一个都需要创建四个 more Fingers等...因此堆栈溢出。

。最简单的方法可能是阻止Finger延长Player - 您确定应该这样做吗?