开发者

Can I construct an Object without knowing the class in ActionScript 2?

开发者 https://www.devze.com 2023-01-09 06:07 出处:网络
In AS2, I can certainly do this: var instance = new MyClass(); But is there a way to do something like this?

In AS2, I can certainly do this:

var instance = new MyClass();

But is there a way to do something like this?

var constructor = MyClass;
var instance = new constructor();

Th开发者_StackOverflowis appears to be possible in AS3 by just calling "new" on an instance of the Class object, but I haven't been able to figure out what the syntax would be to get this working in AS2.


You can do that :

First, you must declare the class for include it in the swf.

var toto:YOUR_CLASS;

And next you can get an instance by :

var instance = new["directory.subdirectory.YOUR_CLASS"]();  

Edit:

<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="vertical" xmlns:at="at.controls.*" creationComplete="init()">
    <mx:Script>
        <![CDATA[
            import mx.rpc.remoting.mxml.RemoteObject;
            private function init():void {
                var obj:Object = ObjectLoader.getInstanceOf(RemoteObject);
            }
        ]]>
    </mx:Script>
</mx:Application>  

ObjectLoader:

package {
    public class ObjectLoader {
        public function ObjectLoader(){
        }

        public static function getInstanceOf(cl:Class):Object {
            return new cl;
        }
    }  

This is a new example. I create an instance of RemoteObject. }

0

精彩评论

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