Coded Creativity
codedcreativity.blogspot.com
codedcreativity.blogspot.com
Creating you first class and packageA quick Summary of OOP ::In the oop model, pieces of code are considered objects and each such object is properly defined by a class with variables and functions specific to the object. Such discrete pieces of code can be easily reused and allows for encapsulation.
Code::package mypackage.as3 {class Ball{import flash.display.*;var x:Number;var y;Number;function Ball(){// some initialization code heretrace("Object Successfully created");}public function changeSize(){// code to change size here}private function secret(){// some code, whose functionality you wish to hide}}}
package mypackage.as3
in this line, we are defining a package, which acts as a repository of all our classes written for the specific application.
class Ball{
import flash.display.*;
in the first line, we are defining our class which contains properties and functions of any Ball object that we use in our program. And the second line imports all classes from the display package so that they could be used in our class.
var x:Number;var y:Number;Now we are creating two variables x and y which will exist for every instance of the ball object which we create in the future.here "var" keyword indicates that we are creating a new variable of name "x" and ":Number" tells it what type of variable it is. In this case, a number.Syntax for Creating a new Variable :: var variableName:variableType;
function Ball(){// some initialization code heretrace("Object Successfully created");}Here, we are defining a constructor. A constructor is a special function which is executed as soon as an instance of an object is created. It is distinguished from other functions as it has the same name as the class.The trace function is one that is heavily used for debugging in flash. This function prints the arguments passed into an output window. In this case, it prints "Object Successfully created".public function changeSize(){// code to change size here}Here, we are defining a normal function by the name changeSize. The function type is set to public (access modifier) which means that it can be accessed from outside the class too.
- AcessModifiers can be prefixed to classes, functions and any kind of variables.
private function secret(){// some code, whose functionality you wish to hide}Here, we are defining another function, only this time, the access modifier set to private which means it can only be called from inside the class.Syntax for Defining a function :: function functionName(parameters-here){ }