Forum Topic: As: Oop - Static Properties/methods

(1,355 views • 4 replies)

This topic is 1 page long.

<< < > >>
None

authorblues

Reply To Post Reply & Quote

Posted at: 2/13/06 10:52 AM

authorblues FAB LEVEL 12

Sign-Up: 06/21/05

Posts: 6,360

trace(TutorialBank.ASMain);

as a continuation of the OOP trend, as of late, ill continue to add information about static properties and methods. static properties are properties such as Math.PI, which can be referenced, but cannot be altered as a normal property of a class.

static methods also follow likewise. in the math class, you dont create a variable datatyped to math, but instead, use it as a static class to modify the number datatype.

firstly, read the following:
AS: OOP (Object Oriented Programming) by Inglor
AS: OOP Classes by authorblues

let us start with a class that we can use to illustrate the point here.

class Encrypt{
// stuff in here
}

now, lets create a static property. this will serve as a general, unchangable variable for us to use later, for whatever reason. consider the value of this to be similar to Math.PI, where you cannot change the value of Math.PI, because it is read-only. so, lets say that we want a static key in our class:

class Encrypt{
public static var key:Number = 1928348;
}

now, lets add a static method, just to demonstrate their use. we will add a generateCode method that will be static, which is essentially a way to link a function with a class without having to define it within the code. everything running modularly, making debugging more efficient.

class Encrypt{
public static var key:Number = 1928348;
public static function generateCode(num:Number):String{
return Math.round(key/num).toString(36);
}
}

as horribly insecure as this encryption method would be, it helps to further demonstrate its use. now, the common way to use a class is to save it, in this case, as Encrypt.as, in the same folder as the fla, and create a variable datatyped to the class name, like so:

var something:Encrypt = new Encrypt();

but for this example, this is not the case. all we need to do is import the class file, and we automatically have access to its properties and methods without a variable pointer. the most common way of doing this is like the following:

var someVal:Number = Math.ceil(Math.random()*1000);
import Encrypt;
trace(Encrypt.key);
trace(Encrypt.generateCode(someVal));

if that made any sense, you should have noticed that we actually used the name of the class directly instead of giving a variable the datatype Encrypt.

SCRIPTED ON THE BBS FOR EXAMPLE ONLY.
i cannot guaruntee the validity of any of this code.

GENERATION 1-i: The first time you see this, copy it into your sig on any forum. Square it, and then add i to the generation.

BBS Signature

None

liaaaam

Reply To Post Reply & Quote

Posted at: 2/13/06 11:23 AM

liaaaam NEUTRAL LEVEL 22

Sign-Up: 12/11/04

Posts: 14,536

At 2/13/06 10:52 AM, authorblues wrote: var someVal:Number = Math.ceil(Math.random()*1000);
import Encrypt;
trace(Encrypt.key);
trace(Encrypt.generateCode(someVal));

You don't need to import the class before using it's static functions =\


None

authorblues

Reply To Post Reply & Quote

Posted at: 2/13/06 11:27 AM

authorblues FAB LEVEL 12

Sign-Up: 06/21/05

Posts: 6,360

At 2/13/06 11:23 AM, -liam- wrote: You don't need to import the class before using it's static functions =\

did not know that. but the idea is all the same =p

GENERATION 1-i: The first time you see this, copy it into your sig on any forum. Square it, and then add i to the generation.

BBS Signature

None

liaaaam

Reply To Post Reply & Quote

Posted at: 2/13/06 11:54 AM

liaaaam NEUTRAL LEVEL 22

Sign-Up: 12/11/04

Posts: 14,536

At 2/13/06 11:27 AM, authorblues wrote: did not know that. but the idea is all the same =p

True, nice tutorial btw.


None

authorblues

Reply To Post Reply & Quote

Posted at: 2/13/06 12:29 PM

authorblues FAB LEVEL 12

Sign-Up: 06/21/05

Posts: 6,360

At 2/13/06 11:54 AM, -liam- wrote: True, nice tutorial btw.

thanks. im still learning these things. i used this very heavily for my matrix class i wrote (to solve N equations in N variables), in order to do matrix multiplication and reduction in a standalone class.

GENERATION 1-i: The first time you see this, copy it into your sig on any forum. Square it, and then add i to the generation.

BBS Signature

All times are Eastern Standard Time (GMT -5) | Current Time: 09:32 AM

<< Back

This topic is 1 page long.

<< < > >>
You need a Grounds Gold Account to post on the NG BBS! If you don't have one, click here to sign up now! It's fast, free, and easy — and opens up tons of great NG features!