00:00
00:00
Newgrounds Background Image Theme

Arnahan just joined the crew!

We need you on the team, too.

Support Newgrounds and get tons of perks for just $2.99!

Create a Free Account and then..

Become a Supporter!

As: Oop - Static Properties/methods

3,964 Views | 4 Replies
New Topic Respond to this Topic

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.


BBS Signature

Response to As: Oop - Static Properties/methods 2006-02-13 11:23:02


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 =\


Sup, bitches :)

BBS Signature

Response to As: Oop - Static Properties/methods 2006-02-13 11:27:47


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


BBS Signature

Response to As: Oop - Static Properties/methods 2006-02-13 11:54:30


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.


Sup, bitches :)

BBS Signature

Response to As: Oop - Static Properties/methods 2006-02-13 12:29:47


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.


BBS Signature