General Question

jmdecombe's avatar

Does not implementing a setter method in ActionScript 3 make the property unsettable?

Asked by jmdecombe (9points) October 6th, 2009

Assuming that I define an ActionScript 3 class as follows:

package com.fluther {
public class Fluther {
private var _property:Object;
public function Fluther (property:Object) {
this.property = property;
}
public function get property ():Object {
return _property;
}
}
}

Does the lack of a set method for property make property unsettable except through the constructor? If that is not the case, how should the set method be implemented so as to make property gettable but not settable (by throwing an IllegalStateError?)?

And assuming that I

Observing members: 0 Composing members: 0

3 Answers

markyy's avatar

Assuming what? I don’t really get your question, you don’t seem like a complete newbie when it comes to actionscript. But the question seems so simple. Is this some issue with flash that I don’t know of? I haven’t used flash in a year or two now, but OOP always stays the same so here goes:

Yes, in your example the only way to change the var _property is by initiating a new instance of the object and pass the value(object) to the constructor. Because you made the scope of the variable _property private it can only be accessed in the scope of the object, not outside of it. That is why you need a setter method when you want to be able to change that value, since you don’t want to do that, declaring the variable _property private is enough.

This is called encapsulation (public, private, etc) and is really the very basic of object orientated programming and as3. Try learning more about encapsulation, for example by reading this tutorial.

jmdecombe's avatar

Thanks. I am indeed not a programming newbie, but my background is in Java and ActionScript 3, to which I just switched for client development, has a bunch of differences that makes me sometimes wonder. Since the getter and setter methods do seem to get special treatment in AS3 (get and set keywords, ability to use dot notation to access properties with such methods), I just wanted to make sure regarding the above. Had I been less lazy, I could have written a little piece of code to test it myself, but I thought, hey, this thing called Fluther is supposed to get you answers really fast so why not try it on a bunch of programming questions and see. Cheers!

markyy's avatar

@jmdecombe Oh, well forget about my post :) Fluther is great, but I noticed programming questions don’t really get that many responses. Stackoverflow is great if you want fast responses on programming questions (which you probably already know about as well).

Answer this question

Login

or

Join

to answer.

This question is in the General Section. Responses must be helpful and on-topic.

Your answer will be saved while you login or join.

Have a question? Ask Fluther!

What do you know more about?
or
Knowledge Networking @ Fluther