Error: Call to a possibly undefined method getRegionNameForCountries through a reference with static type com.framework.model:CountryModel

Error: Call to a possibly undefined method getRegionNameForCountries through a reference with static type com.framework.model:CountryModel

试图找出为什么我可以从这个class的实例化版本中调用这个函数。

我得到的错误是这样的:

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() + ')';
            }

我尝试从中调用函数的 Class 在这里:

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;
            }
    }
}

您只能从 Class 对象本身(例如 MyClass.method())或从 class 声明(静态或实例化)中访问静态 vars/method。

这是一个简化的例子:

MyClass.staticFunction(); //allowed

var myInstance = new MyClass();
myInstance.staticFunction(); //NOT allowed

//inside the MyClass.as
this.staticFunctionInSameClass(); //allowed

您要做的是从对 class.

的实例化对象的引用访问静态方法

要保持​​与当前结构相同的结构,您需要在 class:

中创建一个非静态辅助方法
//CountryModel class
public function getRegionNameForCountriesHelper(countries:Array):String
{
    return getRegionNameForCountries(countries); //calls the static method
}

或者直接在 class 本身上访问它。

return CountryModel.getRegionNameForCountries(item.countriesAvailable, ....);

如果事先不知道 Class,您可以通过将实例转换为 Object,然后访问 constructor 属性 returns 对 Class.

的引用
return (Object(RemoteModelLocator.getInstance().appModel.countryModel).constructor).getRegionNameForCountries(item.countriesAvailable, ...);

虽然没有编译时检查,但这种方式非常混乱。

我建议要么将 class 设为静态(不允许实例化),要么不要在其中使用静态方法。在不知道应用程序的所有这些部分(例如 RemoveModelLocator、appModel)的情况下,很难说什么最适合您的情况。