通过静态类型 Class 的引用调用可能未定义的方法 [英] Call to a possibly undefined method through a reference with static type Class
问题描述
我编写了一个单例类来跟踪应用程序中的一些变量.
I wrote a singleton class to keep track of some variables across my application.
我遇到了一个我无法弄清楚的语法错误,我确信我遗漏了一些简单的东西,但那是那些日子之一.有人发现我的代码有问题吗?
I am getting a syntax error that I can't figure out, I am sure that I am missing something simple but it's been one of those days. Anyone see something wrong with my code?
错误是 1061:通过静态类型 Class 的引用调用可能未定义的方法 setResult.
The error is 1061: Call to a possibly undefined method setResult through a reference with static type Class.
我的单例类中的函数
public function setResult(resultNumber:int, value:int): void
{
switch(resultNumber)
{
case 2: { this.result2 = value; break; }
case 3: { this.result3 = value; break; }
case 4: { this.result4 = value; break; }
case 5: { this.result5 = value; break; }
case 6: { this.result6 = value; break; }
case 7: { this.result7 = value; break; }
case 8: { this.result8 = value; break; }
case 9: { this.result9 = value; break; }
case 10: { this.result10 = value; break; }
case 11: { this.result11 = value; break; }
case 12: { this.result12 = value; break; }
case 13: { this.result13 = value; break; }
case 14: { this.result14 = value; break; }
}
}
我的 mxml 页面中的函数调用
My function call in my mxml page
if(chkBox1.selected == true)
{
utils.Calculation.setResult(2,1);
}
在此先感谢您的帮助!
推荐答案
假设你的单例是 Calculation 类,你是否错过了 getInstance 调用?
Assuming you singleton is the Calculation class, are you missing your getInstance call?
utils.Calculation.getInstance().setResult(2, 1);
一个好的动作单例模式:
A good actionscript singleton pattern:
package com.stackOverflow
{
public class MySingleton
{
public function MySingleton(lock:Class)
{
if(lock != SingletonLock)
throw new Error("This class cannot be instantiated, it is a singleton!");
}
private static var mySingleton:MySingleton;
public static function getInstance():MySingleton{
if(mySingleton==null)
mySingleton = new MySingleton(SingletonLock);
return mySingleton;
}
public function setResult(resultNumber:int, value:int): void{
//...
}
}
}
class SingletonLock{}
计算类的 getInstance() 示例:
getInstance() example for the Calculation class:
private static var calculation:Calculation;
public static function getInstance():Calculation{
if(calculation==null)
calculation = new Calculation(SingletonLock);
return calculation;
}
这篇关于通过静态类型 Class 的引用调用可能未定义的方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!