Due to requirements outside of my control (don't ask, it's ridiculous) I need to create an AS3 class called 'Math' that references the Global AS Math class. So, for example:
由于我无法控制的要求(不要问,这很荒谬)我需要创建一个名为'Math'的AS3类,它引用Global AS Math类。所以,例如:
package my.package
{
public class Math
{
public static function pow( a:Number, b:Number ):Number {
// How do I call the Global.as$Math#pow(..) function?
return Math.pow(a,b);
}
}
}
The code above is clearly wrong - results in infinite recursion. I'm not sure how to say that I want to delegate to the Global.as$Math class and not this Math class...
上面的代码显然是错误的 - 导致无限递归。我不知道怎么说我想委托给Global.as $ Math类而不是这个Math类......
My current awkward solution is to delegate to another class (not named Math) that passes through to the Global Math class. Is there a better way to do this?
我目前的尴尬解决方案是委托另一个传递给Global Math类的类(不是名为Math)。有一个更好的方法吗?
Thanks!
5 个解决方案
#1
Here is another way that popped into my mind after reading Josh Tynjala's post about how package in actionscript are just an abstraction layer over namespaces:
在阅读Josh Tynjala的帖子后,这是另一种突然出现在我的脑海中的方式,关于动作脚本中的包只是名称空间上的抽象层:
public class Math
{
namespace globalNs = "";
public static function pow( a:Number, b:Number ):Number
{
return globalNs::Math.pow(a, b);
}
}
The globalNs::Math.pow explicitly refer to the top level Math Class.
globalNs :: Math.pow显式引用*Math类。
#2
Save a static reference to the flash player Math object and use it throughout your static methods:
保存对Flash播放器Math对象的静态引用,并在整个静态方法中使用它:
package test
{ import flash.utils.getDefinitionByName;
{import flash.utils.getDefinitionByName;
public class Math
{
private static var _flashMath:Class = Class(getDefinitionByName("Math"));
public static function pow( a:Number, b:Number ):Number
{
return _flashMath.pow(a, b);
}
}
}
#3
Try using the AS3 namespace to refer to the AS3 Math object. Or your class could simply extend the Math object and it would automatically have all of the Math object's functionality without you having to rewrite all those wrapper functions.
尝试使用AS3名称空间来引用AS3 Math对象。或者您的类可以简单地扩展Math对象,它将自动拥有所有Math对象的功能,而无需重写所有这些包装函数。
#4
As a small follow up on apphackers reply, you can not simple extends the AS3 Math object and have all it's functionality as was suggeested. Static methods are lost when extending an object since they are statically tied to the object which defines them. Additionally you can't extends a class with the same name. You might have some success with the namespace solution however, though I'm not sure if it'll work with static methods, I'd be interested to see your results.
作为对apphackers回复的一个小跟进,你不能简单地扩展AS3 Math对象,并具有所有的功能,如同建议的那样。扩展对象时静态方法会丢失,因为它们静态地绑定到定义它们的对象。此外,您无法扩展具有相同名称的类。您可能在命名空间解决方案上取得了一些成功,但是我不确定它是否适用于静态方法,我有兴趣看到您的结果。
#5
Math is a special case in AS3 because really it shouldn't be global but it is. So it has no namespace as far as I can tell. The solution you came up with to route through another class is actually very clever. But you know that really the solution is to name the class Math2 or MathHelper or MathUtils rather than Math. Please tell me what the reason beyond your control is! The not knowing is killing me!!!
数学是AS3中的一个特例,因为它实际上不应该是全局的,而是它。因此,就我所知,它没有命名空间。你提出的通过另一个类路由的解决方案实际上非常聪明。但是你知道真正的解决方案是将类命名为Math2或MathHelper或MathUtils而不是Math。请告诉我你无法控制的原因是什么!不知道是在杀我!!!
#1
Here is another way that popped into my mind after reading Josh Tynjala's post about how package in actionscript are just an abstraction layer over namespaces:
在阅读Josh Tynjala的帖子后,这是另一种突然出现在我的脑海中的方式,关于动作脚本中的包只是名称空间上的抽象层:
public class Math
{
namespace globalNs = "";
public static function pow( a:Number, b:Number ):Number
{
return globalNs::Math.pow(a, b);
}
}
The globalNs::Math.pow explicitly refer to the top level Math Class.
globalNs :: Math.pow显式引用*Math类。
#2
Save a static reference to the flash player Math object and use it throughout your static methods:
保存对Flash播放器Math对象的静态引用,并在整个静态方法中使用它:
package test
{ import flash.utils.getDefinitionByName;
{import flash.utils.getDefinitionByName;
public class Math
{
private static var _flashMath:Class = Class(getDefinitionByName("Math"));
public static function pow( a:Number, b:Number ):Number
{
return _flashMath.pow(a, b);
}
}
}
#3
Try using the AS3 namespace to refer to the AS3 Math object. Or your class could simply extend the Math object and it would automatically have all of the Math object's functionality without you having to rewrite all those wrapper functions.
尝试使用AS3名称空间来引用AS3 Math对象。或者您的类可以简单地扩展Math对象,它将自动拥有所有Math对象的功能,而无需重写所有这些包装函数。
#4
As a small follow up on apphackers reply, you can not simple extends the AS3 Math object and have all it's functionality as was suggeested. Static methods are lost when extending an object since they are statically tied to the object which defines them. Additionally you can't extends a class with the same name. You might have some success with the namespace solution however, though I'm not sure if it'll work with static methods, I'd be interested to see your results.
作为对apphackers回复的一个小跟进,你不能简单地扩展AS3 Math对象,并具有所有的功能,如同建议的那样。扩展对象时静态方法会丢失,因为它们静态地绑定到定义它们的对象。此外,您无法扩展具有相同名称的类。您可能在命名空间解决方案上取得了一些成功,但是我不确定它是否适用于静态方法,我有兴趣看到您的结果。
#5
Math is a special case in AS3 because really it shouldn't be global but it is. So it has no namespace as far as I can tell. The solution you came up with to route through another class is actually very clever. But you know that really the solution is to name the class Math2 or MathHelper or MathUtils rather than Math. Please tell me what the reason beyond your control is! The not knowing is killing me!!!
数学是AS3中的一个特例,因为它实际上不应该是全局的,而是它。因此,就我所知,它没有命名空间。你提出的通过另一个类路由的解决方案实际上非常聪明。但是你知道真正的解决方案是将类命名为Math2或MathHelper或MathUtils而不是Math。请告诉我你无法控制的原因是什么!不知道是在杀我!!!