有多種方法可以做到這一點。 Object
方法payam sbr建議非常簡單。
要添加到現有答案中:您可以編寫一個類來表示化學對象值對象(VO)(例如存儲它的名稱,記號,質量等)。):
package {
public class Element {
private var _name:String;
private var _notation:String;
private var _mass:Number;
public function Element(name:String,notation:String,mass:Number) {
_name = name;
_notation = notation;
_mass = mass;
}
public function getName():String{
return _name;
}
public function setName(newName:String):void{
_name = newName;
}
public function getNotation():String{
return _notation;
}
public function setNotation(newNotation:String):void{
_notation = newNotation;
}
public function getMass():Number{
return _mass;
}
public function setMass(newMass:Number):void{
_mass = newMass;
}
public function toString():String{
return "[Element name="+_name+" notation="+_notation+" mass="+_mass+"]";
}
}
}
然後,您可以創建元素的一個維數組和訪問數據的每個元素存儲:
var elements:Vector.<Element> = new Vector.<Element>();
elements.push(new Element("Hydrogen","H",1.01));
elements.push(new Element("Oxygen","O",16.01));
var totalMass:Number = 0;
for(var i:int = 0; i < elements.length; i++){
trace("elements[",i,"]",elements[i]);
totalMass += elements[i].getMass();
}
trace("total mass",totalMass);
輸出:
elements[ 0 ] [Element name=Hydrogen notation=H mass=1.01]
elements[ 1 ] [Element name=Oxygen notation=O mass=16.01]
total mass 17.020000000000003
注意,一個類型的載體將是快於Object
實例的無類型Array
。此外as3.0 getters/setters可能會有點慢,因此代碼java風格的get/set方法。
如果你不打算通過大量的元素和性能不必儘可能緊,你可以使用典型的as3 getter/setter行爲像屬性(如果這是更可讀/更容易理解):
package {
public class Element {
private var _name:String;
private var _notation:String;
private var _mass:Number;
public function Element(name:String,notation:String,mass:Number) {
_name = name;
_notation = notation;
_mass = mass;
}
public function get name():String{
return _name;
}
public function set name(newName:String):void{
_name = newName;
}
public function get notation():String{
return _notation;
}
public function set notation(newNotation:String):void{
_notation = newNotation;
}
public function get mass():Number{
return _mass;
}
public function set mass(newMass:Number):void{
_mass = newMass;
}
public function toString():String{
return "[Element name="+_name+" notation="+_notation+" mass="+_mass+"]";
}
}
}
測試代碼:
var elements:Vector.<Element> = new Vector.<Element>();
elements.push(new Element("Hydrogen","H",1.01));
elements.push(new Element("Oxygen","O",16.01));
var totalMass:Number = 0;
for(var i:int = 0; i < elements.length; i++){
trace("elements[",i,"]",elements[i]);
totalMass += elements[i].mass;
}
trace("total mass",totalMass);
如果你使用一個類與Object
,你還可以得到自動完成。
如果你想要一個選項接近Object
辦法用更少的代碼進行快速原型,你也許可以矇混過關的公共屬性:
package {
public class Element {
public var name:String;
public var notation:String;
public var mass:Number;
public function Element(name:String,notation:String,mass:Number) {
this.name = name;
this.notation = notation;
this.mass = mass;
}
public function toString():String{
return "[Element name="+name+" notation="+notation+" mass="+mass+"]";
}
}
}
什麼?你的意思是自動完成? –
我的意思是我需要一些方法來不必使用數百如果命令,我會編輯問題中的代碼,以便您可以看到它是如何現在 –
我明白了,謝謝 –