1. 程式人生 > >Egret之反射

Egret之反射

Egret 反射

不說了 , 來代碼

一 : 不帶參數的構造函數類

module demo{
    export class NoParamsClass{
        public constructor(){

        }

        public sayHello() : void{
            egret.log(`Hello`);
        }
    }
}

二:帶參數的構造函數類

module demo{
    export class  HasParamsClass{
        private _name : string = null;
        public constructor( $name : string ){
            this._name = $name;
        }

        public sayName() : void{
            egret.log( ` ${this._name}` );
        }
    }
}

重點 , 反射的應用如下

        //不帶參數
        let $no : any = egret.getDefinitionByName( "demo.NoParamsClass" );
        let $noClass : demo.NoParamsClass = new $no();
        $noClass.sayHello();

        //帶參數
        let $has :any = egret.getDefinitionByName("demo.HasParamsClass");
        let $hasClass : demo.HasParamsClass = new $has( "Aonaufly" );
        $hasClass.sayName();

結果:
技術分享圖片

Egret之反射