在 AS3 中从静态函数访问非静态函数时遇到问题

发布于 2024-09-04 07:36:00 字数 574 浏览 13 评论 0原文

我有一个类,其中包含一个下拉菜单。为了节省空间,并且由于菜单的内容永远不会改变,我为填充每个实例菜单的整个类创建了一个静态 DataProvider。我希望用实际函数填充列表,如下所示:

tmpArr.push({label:"Details...", funct:openDetailsMenu, args:""});

然后将 tmpArr 分配给 DataProvider。由于 DataProvider 是静态的,因此包含该代码的函数也需要是静态的,但数组中的函数是非静态的。起初这似乎不是问题,因为当用户单击菜单项时,下拉菜单可以在其父项上调用非静态“executeFunction(funct, args)”。但是,当我尝试编译时,设置 DataProvider 的静态函数无法找到传递的非静态函数。如果编译器相信我,代码就会正常工作!

简单的解决方案是仅传递字符串并使用 switch 语句来调用基于该字符串的函数,但这很大、丑陋、不优雅且难以维护,特别是如果某些内容继承自此类。 更简单的解决方案是使 DataProvider 非静态,但我想知道其他人是否有处理此问题的好方法?使静态函数能够看到它的非静态兄弟?

谢谢。

I have a class containing, among other things, a drop down menu. With the aim of saving space, and since the contents of the menu will never change, I've made a static DataProvider for the whole class that populates each instances menu. I was hoping to populate the list with actual functions like so:

tmpArr.push({label:"Details...", funct:openDetailsMenu, args:""});

and then assign tmpArr to the DataProvider. Because the DataProvider is static the function that contains that code also needs to be static, but the functions in the array are non-static. At first it didn't seem like a problem, because when the user clicks on a menu item the drop down menu can call a non-static "executeFunction(funct, args)" on its parent. However, when I try to compile, the static function setting up the DataProvider it can't find the non-static functions being passed. If the compiler would just trust me the code would work fine!

The simple solution is to just pass strings and use a switch statement to call functions based on that, but that's big, ugly, inelegant, and difficult to maintain, especially if something inherits from this class.
The simpler solution is to just make the DataProvider non-static, but I'm wondering if anyone else has a good way of dealing with this? Making the static function able to see its non-static brethren?

Thanks.

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

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

发布评论

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

评论(2

归属感 2024-09-11 07:36:00

好的,使事物静态的基本原因是如果您想让它独立于实例,例如 as3 中的 Math 函数(您调用 Math.min() 而不是 var math = new Math(); math.min ()...) 这对于参考、重复计算、简单操作(x 值加 10)等很有用。

结合静态和非静态功能的问题是,当调用静态函数时,类有可能此时没有实例,或者(在本例中)存在对编译中有意义的函数的任何引用(如果一个单独的类调用该函数,它将如何引用 openDetailsMenu?)。

您需要做的是要么通过名称获取函数(例如对象“functionname”有效),在数组中创建匿名函数,要么向静态函数添加回调方法,与此类似:

public static function doAction(object:Menu, event:String){
    if(event == "details") object.openDetailsMenu() ;
}

总而言之,您只是添加复杂的层数并没有真正的帮助。如果您只是添加一个类函数并让它们全部执行相同的操作,那么与调用静态函数相比,它不会占用更多的空间或精力。您需要考虑如何以及为何在类中(或类外)使用该函数。

OK, the basic reason for making things static is if you want to make it independant of an instance, for example the Math functions in as3 (you call Math.min() as opposed to var math = new Math(); math.min()...) this is useful for reference, repetetive calculation, simple actions (add 10 to x value) etc.

the problem with combining static and non static functionality is that when calling a static function, there is a possibility that the class has no instance at that point, or (in this case) that there is any reference to the function that would make sense in compilation (if a seperate class called the function, how would it reference openDetailsMenu?).

what you need to do is either go through getting function by name (object"functionname" works for example), make annonymous functions in your array or alternatively add a callback method to your static function something similar to this:

public static function doAction(object:Menu, event:String){
    if(event == "details") object.openDetailsMenu() ;
}

all in all you are just adding layers of complexity that isnt really going to help. if you just add a class function and get them all to do the same action it is not taking more space or effort than if you are calling to a static function. you need to think about how and why the function is going to be used in (or out of) the class.

云裳 2024-09-11 07:36:00

您可以只存储对实例的静态引用,在本例中为 _instance。 (有点像贫民区单例)只是要小心,不要在实例化类之前调用​​静态方法。

/// in your constructor define a static reference handle to the instance
public function ClassName(){
   _instance = this;
}

public static function doSomethingStatic(){
   var varValue = ClassName._instance.someInstanceVariable;
}

you could just store a static reference to the instance, in this case _instance. ( Kind of like a ghetto singleton ) just be careful not to call the static method before the class has been instantiated.

/// in your constructor define a static reference handle to the instance
public function ClassName(){
   _instance = this;
}

public static function doSomethingStatic(){
   var varValue = ClassName._instance.someInstanceVariable;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文