如何使用自定义类来定义在ActionScript 3的对象 [英] How to define an object in actionscript 3 using a custom class
问题描述
大家好,我的问题是我必须能够引用在我的客户对象的某些字段。]
Hi my problem is i have to be able to reference certain fields inside my Customer object.]
我学习AS3的时刻,被教导自定义类,但是我们被教导要使用返回值,我想你可以把它的的toString
方法,有什么我需要的是能够调用一个字段来标识即名称
从数组中的对象字段中的对象,这是我的code
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;
}
}
}
由于某种原因,它不会把code表示整个街区在一起,就像 THIS IS
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 ??
推荐答案
我认为你正在做什么是访问名称
,地址
和 PHONENO
从一个不同的类瓦尔?
I think what you are trying to do is access the name
, address
and phoneNo
vars from a different class?
如果是这样,你必须声明为公开
瓦尔私人而不是
瓦尔。
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屋!