如何使用自定义类在 actionscript 3 中定义对象 [英] How to define an object in actionscript 3 using a custom class
问题描述
我的问题是我必须能够引用我的 Customer 对象中的某些字段.]
Hi my problem is i have to be able to reference certain fields inside my Customer object.]
我目前正在学习 AS3 并正在教授自定义课程,但我们被教导使用 toString
返回值的方法,我猜你可以调用它,我需要的是能够从数组中的对象调用一个字段来标识对象,即 name
字段,这是我的代码
I am studying AS3 at the moment and being taught custom classes, but we are taught to use the toString
method of returning a value i guess you could call it, what i need is to be able to call one field to identify the object i.e. name
field from the object in the array, here's my code
package valueObjects
{
public class Person
{
//instance variables
protected var name:String;
protected var address:String;
protected var phoneNo:String;
public function Person(n:String,a:String,p:String)
{
name=n;
address=a;
phoneNo=p;
}
public function toString():String
{
//returns string
return name+":"+address+":"+phoneNo;
}
}
}
出于某种原因,它不会像这是
some reason it will not put that whole block of code together like THIS IS
那么现在我如何定义它而不是 toString 而是以对象形式??
So now how do i define it not toString but in object form ??
推荐答案
我认为您要做的是访问 name
、address
和 phoneNo
来自不同类的变量?
I think what you are trying to do is access the name
, address
and phoneNo
vars from a different class?
如果是这样,您必须将它们声明为 public
变量而不是 private
变量.
If so, you have to declare them as public
vars instead of private
vars.
public var name:String; //now this can be accessed from other classes: thisClassInstance.name
如果你想让它们在其他类中只读,你必须使用 getter 方法:
If you want to have them read-only from other classes, you have to use a getter method:
protected var name_:String; //local var name for full access;
public function get name():String {
return name_; //this can be access by doing thisClassInstance.name
}
这篇关于如何使用自定义类在 actionscript 3 中定义对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!