christian
christian

Reputation: 25

Private function and private variables in flash as3?

there are classes definitions where all their methods are private my question is why are they private, because i've tried a code wherein i changed all the private access modifier to public and nothing changed!

what if you make an instance of that class with all its method private, how are you be able to access that on another class. coz i know private methods are only accessible to the class where they are declared! will the instance of that class be able to access the superclass' s private methods!

on private variables, it's still not clear to me as to why make a private variable and then use a getter and setter, when you can simply make it public! are these variables called static? cause on the way i understand it private functions and private variables are static? am i right?

i know i'm just really confused! coz i'am just new to flash as3, and been studying it for a week and a half!

Upvotes: 0

Views: 4315

Answers (1)

Marty
Marty

Reputation: 39456

private just means that you can only access the property or method from within that class. It's hard to explain why this is useful, but here are some examples:

Read-only: You might want to have a property that is read only. ie. this property is altered by code within it's containing class, but you want to access the current value from elsewhere with a getter.

Encapsulation: Using private is basically just a way to keep your code clean and structured. If you need to work on a project later, you can clearly see which properties are only altered within the current class and which methods are only called from within the current class. Saves you needing to backtrack and make sure that you don't have a random class elsewhere altering your values.

Another tactic that will need private properties is having a value that you can increment, and having the setter run actions appropriate to that.

Eg.

You have a property called _currentSlide within Slideshow.
You have a setter that looks like so:

public function set currentSlide(num:int):void
{
    _currentSlide = num;

    // do logic for swapping the visual slide to
    // the new slide
}

This means that you could easily go:

myslideshow.currentSlide ++;

To increment the _currentSlide property AND load the next slide into view.

If you want to have properties that are only accessible from within a class AND ALSO classes that extend this class, use protected. This is gives you basically the same read-only capabilities as using private with a getter except that you can still set your properties from extending classes.

Also, static and private are non-related. static means that you can access a property or method without creating an instance of the class. Think of the Math class as an example. random, cos, round and so on are static methods of the Math class.

Here's an example:

public class EG
{
    public static function hello():void
    {
        trace("hello");
    }
}

Now instead of doing this:

var t:EG = new EG();
t.hello();

You can simply do:

EG.hello();

Upvotes: 4

Related Questions