开发者

Bad Experience with extending Classes

I've been programming in ActionScript for a while now even though I barely understand how extending classes exactly work. Most of my projects have been one-file scripts so you can probably imagine trying to work with a 1000+ line file (that's probably too little for some of you :P).

Trying to look through numerous online tutorials have been daunting and or confusing at most, and don't explain how extending actually works. Finding one that describes each essential keyword needed would be nice.

In past projects, I've managed to use two scripts, though not technically extending a class (I believe). For example:

Main.as (Main file)

package {

    import flash.display.Sprite;

    public class Main extends Sprite {

        public function Main () {
            trace(GLOBAL.v开发者_如何学JAVAersion); // "1.0"
        }

    }

}

GLOBAL.as (a helper script in the same directory):

package {

    public class GLOBAL {

        public static var version:String = "1.0";

    }

}

It appears that even though I hadn't specifically imported the GLOBAL.as file, it's still accessible.

What I'm asking from the SO community is: How can I better understand how extending a class works, using files in the same directory and another directory, Use the Main file as a simple "includer" and other files as helpers, such as adding a Sprite to the stage. Hopefully I'm not making this more difficult than it is. But for some reason I just can't "get it".

Other note: I've been working with JavaScript a lot more than I have ActionScript, so any guru's out there wanting to give a tutorial on going from JavaScript to ActionScript would be a plus!


Id suggest looking at some object oriented tutorials in general. Ill give you my view of three elements of object orientation. Classes, extending and objects.

An object is an instance of something. An example of this could be two seperate people using the same system on two seperate computers. They are two seperate objects which are often referred to as instantiation. An object instantiates a class.

A class defines an object and provides ita functionality. An example of a class could be a database class for example which stores all of the connection details. The database class would need to be instantiated by the object in order to fullfill the requirements of the system, e.g. Insert a car make into the database.

Extending a class (more commonly known as class inheritance) actually inherits all of the functionality off the parent class. An example of this could be the database class (which i mentioned earlier) inheriting logic from a users table perhaps? When developing iPhone apps you often inherit specific framework functionality and ammend where necessary to make a bespoke system...

Hope this helps in some way.


Another word for a directory containing scripts is a 'package'. In AS3, files in the same directory or package are automatically available to each other, and so there's no need to explicitly import them. Files in sub-directories are not automatically imported, and neither are files are parent folders.

As far as the structure of your classes and their interactions is concerned, I would recommend becoming familiar with OOP design patterns. The Gang of Four book is an excellent reference although it does not deal specifically with AS3, or alternatively O'Reilly publish a more specific Adobe Developer Library reference.


Firstly, the reason why you don't have to specifically import the GLOBAL.as file is because it is in the same package folder that the other class is. If it was in another package then it would need to be imported.

I like to think of a class as a template where you define your data and functions. Then you create an instance of this object. The instance encapsulates this data and only provides certain public functions. The idea being, other external instances can communicate with another instance through a predefined interface, like a contract, where it says, if you call this function and pass this in, I will return this. The actual implementation of how this class does this is hidden, thus, it is easy to change the inner workings without affecting the rest of the system.

Extending a class is also known as inheriting. If a class needs common functions and data, instead of having two separate classes with some similar functions and some different functions, we can instead have a base class which has functions common to both. Then the sub class can extend this, so it has these functions, as well as adding its own. This way, if you change the function in the base class, then all classes extending this will automatically use this newly changed function, thereby reducing maintenance and bugs.

As for using classes in another directory, we use the package syntax to define where a class is.

For example: com.shaz.physics.MyClass

You would have a folder called com. Inside that folder would be the folder, shaz, inside that the folder physics, and inside that your MyClass.as file. In that MyClass.as file the package would be package com.shaz.physics. Other classes in other folders, would then need to import your class as: import com.shaz.physics.MyClass

Finally, if you need helper classes, then making the functions static can be a good way. For example, if you had a Math class to do something you could define it as:

public class Math
{
    public static function doSomething():void
    {

    }   
}

Then instead of creating an instance of the object, you can just do Math.doSomething();


extending classes in ActionScript 3.0 is called Inheritance in OOP.

if you look at the ActionScript 3.0 documentation, you can see which classes extend or subclass from other classes. let's take MovieClip for example:

Bad Experience with extending Classes

MovieClip is an extension of Sprite, which is an extension of DisplayObjectContainer, which is an extension of InteractiveObject, etc.

this means that MovieClip not only contains it's own native functions and properties but also all functions and properties of Sprite and all the functions and properties that Sprite inherited from DisplayObjectContainer as well, and all the functions and properties within the other classes going all the way down the line to Object.

therefore, if you create a class that extends Sprite, you will automatically have access to all of Sprite's native and inherited functions and properties. however, you won't have access to MovieClip's functions and properties since Sprite is a subclass of MovieClip.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜