What Are Getters And Setters?
Probably not the right place to ask, but what exacly are setters and getters? I didn't quite understand the explanation in the language reference.
KirupaForum > Flash > ActionScript 3.0
Posted on: 09-28-2007, 01:54 PM
View Complete Forum Thread with Replies
See Related Forum Messages: Follow the Links Below to View Complete Thread
Custom Setters And Getters
I've noticed in a lot of people's classes that they prepend variables with an underscore and use custom getters and setters.
Why is this? Is it for performance reasons? How/why is a custom getter better then getting a variable normally?
Getters/setters Vs Methods - Which To Use When?
Hi there,
When is the best way to use getters/setters, and when to use methods?
Suppose i have a Class that defines
ActionScript Code:
public function setData( someData ):void {}
and i get an idea of rather using
ActionScript Code:
public function set data( someData ):void {}
So an example:
Lets assume there is only one parameter - the someData parameter - and it can be other class instance (data class with more attributes), or Object, or String, or whatever.. (if there would be more parameters, we would use a method, not a setter).
Lets further assume, that the method is used to update a variable, and then calls a function that will refresh the whole view from that variable.
Questions:
1. what methodology would be appropriate and why? setters/methods?
2. Is there a difference (speed impact/broken OOP/..) between using get/setters and using methods? (I read somewhere that get/set break the OOP and that using methods is recommended..) - pls include source/links for this is you have any
Thanks!
Proper Use Of Setters/getters?
Hey all,
Is it improper to put functionality other than setting or getting data in your getter/setter functions? I just wrote my Connector class ( http://www.kirupa.com/forum/showthread.php?t=268245) and for example, in my linethickness setter, I have:
Code:
public function set lineThickness(value:uint):void {
_lineThickness = value;
drawTriangle();
drawConnector();
}
Is calling my draw methods in the setter improper? If so, what's the alternative?
[AS3] Overriding Setters/Getters
Hi, guys.
Is it possible to override the x, y, width, and height properties ( setters/getters ) of the DisplayObject class, or alternatively watch for value changes?
I would like to add custom events ( "move" and "resize" ) to a class that extends Sprite, but I'm having trouble working out the best way to catch changes to the x, y, width, and height property values.
I could use a couple of methods I guess ( setPosition, setSize, etc ) but that won't prevent someone changing the property values through the setters/getters.
Any ideas?
I'm sure you know what I'm talking about but here's a quick example:
ActionScript Code:
var box:BoxThing = new BoxThing();
box.addEventListener(BoxThingEvent.MOVE, handler);
box.addEventListener(BoxThingEvent.RESIZE, handler);
// The following should cause an event to be dispatched...
box.x = 100;
box.width = 200;
AS3: Override Setters Getters Dilema
In AS2 my class (which extended the MC) had setters and getters for x, y, z and would then make calculations and set the instance's _x, _y values based on what I set as x, y, & z. It worked beautifully.
In AS3, I am extending Sprite. Now I had planned to use the same getter and setters, but since Adobe/MM deprecated the _x, _y props for x, y (which I am glad they did), it presents a new problem.
How in the hell can I do this:
ActionScript Code:
public function set x (newValue:Number):Void { this.x = calculateNewX (newValue):Number; //used to be this._x = calculateNewX (newValue):Number;}
Maybe the answer is painfully obvious such as just reverse your desired accessors to _x, _y, _z. Well darnit, I would like not to do that. Any other suggestions.
MovieClip SubClass: Getters, Setters And MouseEvents
Hi folks. I am plain stumped. I am working in F8/as2.0.
I have a class that extends the MovieClip class. I have a blank_mc in the library that is linked to the class. Here is the constructor:
ActionScript Code:
public function MyMC (target_mc:MovieClip){
var _mc:MovieClip = this;
_mc = target_mc.attachMovie (.....);
}
That works all fine and dandy. Where I am having issues is basically I have properties x, y that DON't overwrite the _mc's _x & _y props. Instead, the use getter & setters that take values and then calculate the needed number to set the actuall _mc._x, _y props. This doesn't seem to work at all and i don't know why. Here is the code:
ActionScript Code:
function set x (num):Void {
__x = num;
this._x = (do some calculations based on num);
}
Of course this is really generalized. But is __x a private property of the MovieClip class and I am interfering with it?
On to issue two...
So I got this to work by NOT extending the MC. Instead I have a variable ref to a _mc prop in the class. However I am unable to set event functions to it even if I make some ref to the _mc prop. Is there a good way of approaching this by say a decorator pattern?
Flex Brain Block: Dynmaic Getters And Setters
Either I'm having a brain block on creativity, or my googling skills have been compromised today. I want to be able to dynamically add get and set functions to an existing instance of a builtIn class.
I.E., I want to add a get and set function to the x and y properties of the Image class, so I may tweak the values. I don't want to add these listeners to a subclass and instantiate all that pain and misery (Pun intended), and it must be late, because I'm not grasping how to do it with Proxy right this moment. who wants to make me feel like an idiot and point out the obvious? Much appreciated.
Calling Getters/setters Inside The Constructor? Best Practice?
What is the best way to use the getters/setters inside of a constructor?
ActionScript Code:
//a simple class that draws a line from the origin...package au.com.scottcampbell.draw{ import flash.display.Sprite; public class line extends Sprite{ //Private vars private var _c:uint;//color private var _a:uint;//alpha private var _w:Number;//width private var _h:Number;//height public function line(nw:Number = 0,nh:Number = 0,nc:uint = 0x000000,na:uint = 100){ a(na);//set the alpha c(nc);//set the color w(nw);//set the width h(nh);//set the height render();//we want it rendered off the bat? yes } public function render():void{//update the graphics graphics.clear(); graphics.lineStyle(0,_c,_a*0.01); //calculate the alpha into a decimal graphics.moveTo(0, 0); graphics.lineTo(_w, _h); } // Getters/Setters public function set a(n:Number):void{ if(n > 100){//make sure the alpha is less then 100% _a = 100;//should i cap this to 255 instead? }else if(n < 0){//make sure its greater then 0% _a = 0; }else{ _a = n; } //render(); //should we render it once we have set the a value? } public function get a():uint{ return _a; }//...etc..etc...
I'm trying to improve my oops/as3 so try be as critical as possible.
Also whats a good naming convention for the parameters for the constructor. I start them all with "n" for new. (which i dont feel is a good way of doing things)
MX - Getters And Setters - Class Within A Class
So I have these two classes, Controller and Data
Controller is associated with an empty movie clip so it can run onEnterFrame.
Data is just a data holder with some processing and loading functions and is created within Controller.
Now what I tried to do is write a Getter in Data to be called from Controller. The weird thing is, this method is never even initiated. I can call a number of other Data methods from Controller, but anything relating to Setting or Getting data just doesn't work. I can however target the variable directly, and it works fine.
Why is this happening?
Delegate A Setters Call In AS2?
Code:
_navComp.selectionCallback = Delegate.create( _codeHintsManager, _codeHintsManager.arrowUpCB, true );
Above is the code that i am currently using... I had to make a function because of the scope of my object. I would prefer using the _codeHintsManager.arrowUp (setter). But I can't get anything to work.
Code:
_navComp.selectionCallback = Delegate.create( _codeHintsManager, _codeHintsManager.arrowUp, true );
Above does not work... is there a way to do this without making callbacks?
Getter/Setters Or Public Methods?
When you all write your classes, and you want to expose certain elements of your class to outside parties etc. Do you generally use getter/setters or public functions?
To me a getter/setter is for adjusting a property of your class where as a method is more for say adjusting its behaviour state. For instance, a method called classInstance.play() that plays a video file makes more sense than say a setter classInstance.play = true.
But then, in some cases the distinction is less defined. For instance, changing the volume of something, volume is a property, and seems to fit with a getter/setter pair well: classInstance.volume = .1 as opposed to classInstance.volume(.1).
I guess its a matter of style, but are there any 'hard and fast' rules about when to use a getter/setter verses when to use a public method?
(I realise that getters/setters are also public)
AS3 - Setters Problem (error 1061)
hi all - this is a little strange... I'm trying to access a setter method in a class from another class and getting the error:
1061: Call to a possibly undefined method updateCounter through a reference with static type CounterDisplay.
here's the code I'm using to call the setter:
Code:
private function loadNextQuestion(e:Event):void{
if (numberOfQuestions>(myCounter)){
myCounterDisplay.updateCounter(myCounter+1);
makeQuestion(myCounter)
}else{
trace("done");
}
}
here's the code in the separate class (instantained by myCounterDisplay) of the setter:
Code:
public function set updateCounter(n:int):void{
counterText.htmlText = n+"/"+numberOfQuestions;
//trace(newQuestionNum);
return;
}
if I eliminate the keyword "set" it works fine... but I'm puzzled about why it doesn't work as a setter...
any ideas?
|