I'm trying to define a custom Error subclass in AS3 and would like to override the name property as I have some error handling code that logs errors based on the type (name).

package com.company {

    public class MyError extends Error {
        public function MyError() {
            super("Some description.");
        }

        // Error 1023: Incompatible override
        public override function get name():String { 
            return "MyError";
        }
    }

}

However I get "Error 2013: Incompatible override" on the get name override.

Does anyone know the correct syntax for this? As far as I can tell it seems correct based on the name property in the Error class. The intended result is:

var e:Error = new MyError();
trace(e.name); // Should be "MyError"

Thanks!

有帮助吗?

解决方案

name is defined as public var name:String. So it's a public property and no getter method is defined. So you can't override the getter. To change the name you can just assign your name after calling super's constructor.

public function MyError() {
    super("Some description.");
    name = "MyError";
}
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top