How to define an object in actionscript 3 using a custom class

跟風遠走 提交于 2019-12-12 00:59:59

问题


Hi my problem is i have to be able to reference certain fields inside my Customer object.]

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

So now how do i define it not toString but in object form ??


回答1:


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

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
}


来源:https://stackoverflow.com/questions/17016287/how-to-define-an-object-in-actionscript-3-using-a-custom-class

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!