Actionscript 3 错误:通过静态类型为com.framework.model:CountryModel的引用调用可能未定义的方法getRegionNameForCountries
试图找出为什么我可以从这个类的实例化版本调用这个函数 我得到的错误是:Actionscript 3 错误:通过静态类型为com.framework.model:CountryModel的引用调用可能未定义的方法getRegionNameForCountries,actionscript-3,flash,apache-flex,actionscript,Actionscript 3,Flash,Apache Flex,Actionscript,试图找出为什么我可以从这个类的实例化版本调用这个函数 我得到的错误是: Error: Call to a possibly undefined method getRegionNameForCountries through a reference with static type com.framework.model:CountryModel. 错误来自以下代码: public static function territoriesFunction( item:Object, column
Error: Call to a possibly undefined method getRegionNameForCountries through a reference with static type com.framework.model:CountryModel.
错误来自以下代码:
public static function territoriesFunction( item:Object, column:DataGridColumn ):String
{
return RemoteModelLocator.getInstance().appModel.countryModel.getRegionNameForCountries( item.countriesAvailable ) + ' ('+ item.countriesAvailable.length.toString() + ')';
}
我试图从中调用函数的类如下所示:
package com.framework.model
{
import com.adobe.cairngorm.vo.IValueObject;
import com.adobe.crypto.MD5;
import com.vo.RegionVO;
import flash.utils.ByteArray;
import mx.utils.ObjectUtil;
public class CountryModel implements IValueObject
{
public static function getCountriesForRegion( regionName:String ):Array
{
try
{
var result:Array = _dataModel[regionName];
}
catch(e:Error){}
result = ( result )? result: _dataModel[CountryModel.WORLDWIDE];
return ObjectUtil.copy( result ) as Array;
}
public static function getRegionNameForCountries( countries:Array ):String
{
if( !countries || !countries.length )
{
return CountryModel.WORLDWIDE;
}
countries.sortOn("name");
var buffer:ByteArray = new ByteArray();
buffer.writeObject(countries);
buffer.position = 0;
var hash:String = MD5.hashBytes( buffer );
try
{
var regionName:String = _dataModel[hash];
return ( regionName && regionName.length )? regionName : CountryModel.CUSTOM;
}
catch( e:Error )
{
}
return CountryModel.CUSTOM;
}
}
}
只能从类对象本身(例如
MyClass.method()
)或从类声明(静态或实例化)中访问静态变量/方法
以下是一个简化的示例:
MyClass.staticFunction(); //allowed
var myInstance = new MyClass();
myInstance.staticFunction(); //NOT allowed
//inside the MyClass.as
this.staticFunctionInSameClass(); //allowed
您试图做的是从对该类的实例化对象的引用访问静态方法
要保持与当前相同的结构,您需要在类中创建一个非静态帮助器方法:
//CountryModel class
public function getRegionNameForCountriesHelper(countries:Array):String
{
return getRegionNameForCountries(countries); //calls the static method
}
或者只是在类本身上访问它
return CountryModel.getRegionNameForCountries(item.countriesAvailable, ....);
如果事先不知道该类,可以通过将实例强制转换为对象
,然后访问构造函数
属性来实现,该属性返回对类的引用
return (Object(RemoteModelLocator.getInstance().appModel.countryModel).constructor).getRegionNameForCountries(item.countriesAvailable, ...);
但是这种方法非常混乱,没有编译时检查
我建议您要么只将类设置为静态(不允许实例化),要么不要在其中使用静态方法。如果不知道应用程序的所有这些部分是什么(例如RemoveModelLocator、appModel),就很难说什么最适合您的环境 根据给出的代码,我觉得很好。问题一定在别的地方。RemoteModelLocator.getInstance().appModel.countryModel确实是您所期望的类型吗?当我从所有函数和属性中删除static时,我在应用程序的其他部分中收到大量错误,这些错误告诉我,我在该类上引用了其他可能未知的方法?这里没有人知道您还有哪些其他引用。无论您在哪里执行CountryModel.getRegionNameForCountries
如果在其定义中去掉static关键字,则必须对其进行更改。如果你不想或者没有时间重构你的应用程序,使这些方法不是静态的,那么就从我建议的两个选项中选择一个吧。我将研究为我现在遇到未定义方法错误的两个地方创建一些非静态解决方案。当我从方法中删除static时,应用程序的其余部分就会崩溃。为什么不能返回CountryModel.getRegionNameForCountries(item.countriesAvailable)+'('+item.countriesAvailable.length.toString()+')
在您的territoriesFunction
?@Brian-事实上,我假设直到运行时才知道该类,但我想如果真是这样,为什么他们会访问这个非常特定的方法。