gpt4 book ai didi

flash - 将方法添加到 as3 类

转载 作者:行者123 更新时间:2023-12-04 16:11:48 25 4
gpt4 key购买 nike

as3中有一个Point(flash.geom.Point)类我想将方法​​添加到类 Point(例如 convertToStagePointMyMethod()),我想通过使用调用此方法

var a:Point=new Point();
a.convertToStagePointMyMethod()

我应该怎么做才能将此方法添加到类 Point 中?没有继承是否可能。我想像 .NET 中的“部分”类一样使用它

最佳答案

这是一个使用 prototype 的简单示例:

Point.prototype.foo = function (arg:String):void{
trace("foo called");//foo called
trace("arg",arg);//this is a test
trace("x",this.x);//7
trace("y",this.y);//54
};

var p:Point = new Point(7,54);
p["foo"]("this is a test");

这是不扩展类的唯一方法。另请注意,如果您尝试使用 p.foo("test"),编译器将不会在严格模式下编译,这就是我编写 p["foo"]("test") 的原因 在我的示例中。

关于flash - 将方法添加到 as3 类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5524250/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com