开发者

add method to as3 class

开发者 https://www.devze.com 2023-02-21 07:36 出处:网络
There is a Point (flash.geom.Point) class in as3 I want to add method to class Point (e.g. convertToStagePointMyMethod()) and I\'d like to call this method by using

There is a Point (flash.geom.Point) class in as3 I want to add method to class Point (e.g. convertToStagePointMyMethod()) and I'd like to call this method by using

var a:Point=new Point();
a.convertToStagePointMyMethod()

What should I do in order to add this method t开发者_StackOverflow中文版o class Point? Is it possible without inheritance. I'd like to use it like "partial" classes in .NET


Here is a quick example using prototype :

Point.prototype.foo = function (arg:String):void{
    trace("foo called");//foo called
    trace("arg",arg);//this is a test
    trace("x",this.x);//7
    trace("y",this.y);//54
};

var p:Point = new Point(7,54);
p["foo"]("this is a test");

This is the only way to do this without extending a class. Also note that the compiler will not compile in strict mode if you try to use p.foo("test"), that's why i wrote p["foo"]("test") in my example.


Aside from using OXMO456's really cool example (never seen that before, really cool) you need to use either inheritance or composition. If the class were "dynamic" like the MovieClip class or the URLVariables class you wouldn't need to worry, but Point is not dynamic.

You can make your own dynamic classes by doing:

dynamic public class MyClass {...}


Make your own point class

package 
{
    import flash.geom.Point;

    /**
     * ...
     * @author Jesse Nicholson
     */

    public class MyPoint extends Point 
    {
        public function MyPoint(positionX:Number, positionY:Number) {
            //Pass the constructor args to the super class, the Point class since it requires these params in it's constructor
            super(positionX, positionY);
        }

        public function convertToStagePointMyMethod():Number {
            //Do my calculations here
            var someNumber:Number = 10;
            return someNumer;

            //OR return a Point OR do whatever the hell you want here, you're the boss of your own point class
        }
    }

}

This is just plain the way you do things. When you have an existing class that you'd like to use but just add on to, extend it into a new class and do just that. This is a basic idea in object oriented programming and the "best practice" approach.


Rather than extending a class, I usually create standalone utility functions, like this:

package com.example.utils
{
    public function doSomethingToPoint(p:Point):void
    {
        //do something
    }
}

Put it in a file called, doSomethingToPoint.as, and you're good to go. It works just like built-in package-level functions like flash.net.navigateToURL. Just import and use the function directly.

import com.example.utils.doSomethingToPoint;

var p:Point = new Point();
doSomethingToPoint(p)

Alternatively, you could create a utility class with multiple related static functions, like this:

package com.example.utils
{
    public class PointUtil
    {
        public static function doSomethingToPoint(p:Point):void
        {
            //do something
        }

        //other functions here
    }
}

You'd use the class and its functions like this:

import com.example.utils.PointUtil;

PointUtil.doSomethingToPoint(p);

Personally, I prefer to create standalone functions so that my projects don't need to compile in extra static functions that aren't actually used anywhere, but everyone has their own preference.

0

精彩评论

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

关注公众号