繁体   English   中英

尝试了解Flex中的类

[英]Trying to understand classes in Flex

我正在开发我的第一个真正的Flex应用程序,并从中学到了很多东西。 现在,我试图理解使用类的基础知识。 我有一个从LastFm API询问信息的函数。 这是基本功能:

        public function zoekChart(event:MouseEvent):void
        {
            var api_URL:String = 'http://ws.audioscrobbler.com/2.0/';
            var api_method:String = 'geo.getMetroUniqueArtistChart';
            var api_key:String = '99f1f497c0bd598f1ec20571a38e2219';
            var country:String = countryText.text;
            var metro:String = metroText.text;
            var limit:String = '5'; 
            api_request = api_URL + '?method=' + api_method + '&country=' + country + '&metro=' + metro + '&api_key=' + api_key + '&limit=' + limit;
            myRequest.send();
        }

现在,我正在尝试制作一个与功能相同的类。 这是我到目前为止所拥有的:

package valueObjects
{
public class Kevin_myChart
{
    private var api_URL:String;
    private var api_method:String;
    private var api_key:String;
    private var country:String;
    private var metro:String;
    private var limit:String;

        public function lastFMCall (api_URL:String, api_method:String, api_key:String, 
                                     country:String, metro:String, limit:String) 
        {
            this.api_URL=api_URL;
            this.api_method=api_method;
            this.api_key=api_key;
            this.country=country;
            this.metro=metro;
            this.limit=limit;
        }

        public function getInfo(size:String):String 
        {

            return api_URL + '?method=' + api_method + '&country=' + country + '&metro=' + metro + '&api_key=' + api_key + '&limit=' + limit;

        }   
}

}

这是一个好的开始吗? 我的第一个问题是如何在类中导入textfields countryText和metroText的值。

另外,我应该从这里继续吗? 我如何确保我的应用程序能够使用在类中声明的函数,现在如何在类的vars中获取值?

您可以使用类的constructor来传递值。 创建类的新实例时,将调用构造函数。

public function zoekChart(event:MouseEvent):void
{
    var api_URL:String = 'http://ws.audioscrobbler.com/2.0/';
    var api_method:String = 'geo.getMetroUniqueArtistChart';
    var api_key:String = '99f1f497c0bd598f1ec20571a38e2219';
    var country:String = countryText.text;
    var metro:String = metroText.text;
    var limit:String = '5'; 

    // create a new instance of your class
    var kevinMyChart:Kevin_myChart = new Kevin_myChart(api_URL, api_method, api_key:, country, metro, limit);

    // not sure what the 'size' is used for
    api_request = kevinMyChart.getInfo("size");
    myRequest.send();
}

您的带有构造函数的Kevin_myChart类:

package valueObjects
{
    public class Kevin_myChart
    {
        private var api_URL:String;
        private var api_method:String;
        private var api_key:String;
        private var country:String;
        private var metro:String;
        private var limit:String;

        public function Kevin_myChart(api_URL:String, api_method:String, api_key:String, country:String, metro:String, limit:String) 
        {
            this.api_URL=api_URL;
            this.api_method=api_method;
            this.api_key=api_key;
            this.country=country;
            this.metro=metro;
            this.limit=limit;
        }

        public function getInfo(size:String) : String 
        {
            // not sure what the 'size' is used for    
            return api_URL + '?method=' + api_method + '&country=' + country + '&metro=' + metro + '&api_key=' + api_key + '&limit=' + limit;

        }   
    }
}

您朝着正确的方向前进,也许本教程将为您提供有关类构建方式的更多信息。

http://www.kirupa.com/developer/as3/classes_as3_pg3.htm

否则,快速的Google搜索也可以帮助您。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM