开发者

Event.clone and Event.toString examples?

Can anyone provide more-or-less real-world examples of when you might want to actually use these two methods of the Event class?

AS 3 makes us override these methods in any 开发者_运维知识库custom class that extends Event, but I wonder why.


toString - I only ever use this in debugging messages, but in these cases if you don't override toString then you will not be able to return a string representation of your event. Consider this example, where a single handler is used for catching a variety of event types:

function handleEvents(evt:*):void
{
    //trace the event type and all parameters including extra parameters from custom events
    trace(evt.toString()); 
}

clone - If you need to re-dispatch an event, the event class's clone method will be called. This returns a new event object with the target reassigned to the new dispatching object. The gotcha is that if you don't override the clone method, clone will create a new event object from the last class that defined it, not from your custom event class. This will most likely result in a type error when you catch the re-dispatched event.


adding to shanethehat's toString explanation, if you don't override toString and trace the event it will always print default information of a general Event since it is what your custom event is subclassing.

overriding toString is used for debugging so it's not necessary, although it's encouraged, especially if you are distributing your API, working with other developers or simply want to write clean, consistant code.

depending on the amount of properties within your custom event, you could create a switch statement so that toString returns only appropriate event properties for each type. here's an example of doing so from my HistoryEvent class:

package com.mattie.events
{
    //Imports
    import flash.events.Event;

    //Class
    public class HistoryEvent extends Event
    {
        //Constants
        public static const CHANGE:String = "change";
        public static const STATUS:String = "status";

        //Variables
        public var action:String;
        public var name:String;
        public var data:Object;
        public var undoable:Boolean;
        public var undoableName:String;
        public var redoable:Boolean;
        public var redoableName:String;
        public var index:uint;

        //Constructor
        public function HistoryEvent(type:String, action:String = null, name:String = null, data:Object = null, undoable:Boolean = false, undoableName:String = null, redoable:Boolean = false, redoableName:String = null, index:uint = 0) 
        {
            super(type);

            this.action = action;
            this.name = name;
            this.data = data;
            this.undoable = undoable;
            this.undoableName = undoableName;
            this.redoable = redoable;
            this.redoableName = redoableName;
            this.index = index;
        }

        //Override clone
        override public function clone():Event
        {
            return new HistoryEvent(type, action, name, data, undoable, undoableName, redoable, redoableName, index);
        }

        //Override toString
        override public function toString():String
        {
            switch (type)
            {
                case CHANGE:    return formatToString("HistoryEvent", "type", "action", "name", "data");
                case STATUS:    return formatToString("HistoryEvent", "type", "undoable", "undoableName", "redoable", "redoableName", "index");
                default:        return null;
            }
        }
    }
}


I've never overriden them and I always use custom events whenever it is warranted ?

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜