AS3 覆盖常量

发布于 2024-08-25 23:26:54 字数 359 浏览 6 评论 0原文

我正在尝试执行以下操作:

public class character extends entity {
public const type:int = CHARACTER;
}


public class entity extends Sprite {
public const type:int = INVALID;

public const INVALID:int = -1;
public const CHARACTER:int = 1;
}

但编译器会抛出:

错误:与命名空间 public 中的继承定义 dieEngine:entity.type 存在冲突。公共常量类型:int = CHARACTER;

I am trying to do the following:

public class character extends entity {
public const type:int = CHARACTER;
}


public class entity extends Sprite {
public const type:int = INVALID;

public const INVALID:int = -1;
public const CHARACTER:int = 1;
}

But the compiler throws:

Error: A conflict exists with inherited definition dieEngine:entity.type in namespace public. public const type:int = CHARACTER;

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

染年凉城似染瑾 2024-09-01 23:26:54

好吧,无论是否继承,常量都是常量,并且不应该被重写,可以将其想象为定义两倍于所需常量的完整类。

如果你确实想覆盖它,你应该设置 var 而不是 const

使用:

public var type:int = INVALID;

而不是:

public const type:int = INVALID;

well, constants are constants no matter if they are inherited or not and should not be overriden, think of that like the a complete class which defines two times the required constant.

If you really want to override it, you should set that varinstead of const

Use:

public var type:int = INVALID;

instead of:

public const type:int = INVALID;
婴鹅 2024-09-01 23:26:54

这有帮助吗?您可以将 MovieClip 的属性设置为可以更改的常量:

package
{
    import flash.display.*;

    public class Controller extends MovieClip
    {
        public const IS_VALID:Boolean = true;
        public var type:MovieClip = new MovieClip;

        public function Controller()
        {
            extendedController();
        }

        protected function extendedController():void
        {

        }
    }
}

然后在扩展类中,

package
{
    public class ControllerExtended extends Controller
    {

        public function ControllerExtended()
        {           

        }

        override protected function extendedController():void
        {
            type.IS_VALID = false;

            trace(type.IS_VALID);

            type.IS_VALID = true;

            trace(type.IS_VALID);
        }
    }
}

如果您愿意,可以将常量设置为整数

Does this help at all? You can make the property of a MovieClip a constant that you can change:

package
{
    import flash.display.*;

    public class Controller extends MovieClip
    {
        public const IS_VALID:Boolean = true;
        public var type:MovieClip = new MovieClip;

        public function Controller()
        {
            extendedController();
        }

        protected function extendedController():void
        {

        }
    }
}

Then in your extended class

package
{
    public class ControllerExtended extends Controller
    {

        public function ControllerExtended()
        {           

        }

        override protected function extendedController():void
        {
            type.IS_VALID = false;

            trace(type.IS_VALID);

            type.IS_VALID = true;

            trace(type.IS_VALID);
        }
    }
}

You can make the constant an integer if you wish

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文