我正在开发我的第一个真正的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的值。
另外,我应该如何从这里继续?如何确保我的应用程序能够使用类中声明的函数以及如何在类的变量中获取值?
答案 0 :(得分:1)
您可以使用班级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 Search可以帮助你进一步。