1

ここに私のコード行があります:

  new (getDefinitionByName(String( "mypackage.MyDynamicClass"  )) as Class) ;

これによりエラーが生成されます: mypackage.MyDynamicClass が定義されていません。

私はグーグルで解決策を見つけました:インポートステートメントでクラスの名前を使用してください。

そう、

  import mypackage.MyDynamicClass 
  new (getDefinitionByName(String( "mypackage.MyDynamicClass"  )) as Class) ;

出来た!!!

しかし、動的クラスの利点を実際に侵害しているため、この解決策には満足していません。( import で) クラスの名前を知っている場合、なぜそれを文字列として使用するのでしょうか?

クラスの動的ロードを機能させるための代替手段はありますか?

ヴィシュワス

4

3 に答える 3

5

次の記事をご覧ください。http://blogs.adobe.com/cantrell/archives/2010/09/loading-classes-dynamically-in-actionscript-3.html 実行時に必要なクラスを含めることは避けられません。いつでも使用できます:

import mypackage.*

パッケージにすべてのクラスを含めると、コードサイズが大きくなる可能性があることに注意してください。

于 2013-01-10T16:40:55.090 に答える
3

コンパイラーがプロジェクト内の未使用のクラス/インポートステートメントを検出し、それらのクラスファイルを削除してswcまたはswfとしてパックするため、クラスの動的ロードに違反することは絶対にないため、最終的な出力ファイルサイズの利点を減らすことができます.

数回ファイルサイズを縮小できない場合を除きます。

おそらく、 getDefinitionByName() を介してロードしようとしているクラスが何であるかを知ってから、そのクラスが利用可能であることを確認してくださいIncludeClass.as

より良い方法として、プロジェクトのステートメントをインポートできます

package
{
    public class IncludeClasses
    {
        import com.abc.db.Database; Database;
        import com.abc.RemoteLogTarget; RemoteLogTarget;
        import com.abc.LocalLogTarget; LocalLogTarget;
        import com.abc.exception.GlobalExceptionHandler; GlobalExceptionHandler;
        import com.abc.utils.NetConnectionMonitor; NetConnectionMonitor;
    }
}

あなたのやり方をもっとうまく使いたいなら、コンパイラオプションを試してみてください。

于 2013-01-05T07:55:10.697 に答える
2

抽象ファクトリ http://en.wikipedia.org/wiki/Abstract_factory_patternを使用することをお勧めし ます。これは、インスタンス化するクラスを知らなくても、オブジェクトを作成するための非常に柔軟な方法です。

オブジェクトを作成する可能性のあるすべてのクラスをインポートする必要があるこの抽象ファクトリクラスにあります。これには、メインクラスのすべてのクラスをインポートする必要はありませんが、通信するために抽象ファクトリとインターフェイスをインポートする必要があります。新しいオブジェクトで。

簡単な例を次に示します。

/*
The interface you will use to communicate with your objects
*/
InterfaceForAllMyClasses.as
public interface InterfaceForAllMyClasses
{
    public function callMe();
    public function callMe2();
}

/*
Your Classes wich implement the interface
*/
Class1.as
import mypackage.InterfaceForAllMyClasses;
public class Class1 implements InterfaceForAllMyClasses
{
    public function callMe() { trace("called Class1"); }
    public function callMe2() { trace("called Class1 too"); }
}

Class2.as
import mypackage.InterfaceForAllMyClasses;
public class Class1 implements InterfaceForAllMyClasses
{
    public function callMe() { trace("called Class2"); }
    public function callMe2() { trace("called Class2 too"); }
}

/*
The Abstract Factory
*/
AbstractFactory.as
import mypackage.InterfaceForAllMyClasses;
public class AbstractFactory
{
    public function giveMeObject(classNumber:Number):InterfaceForAllMyClasses
    {
        switch(classNumber)
        {
            case 0: return(new Class1()); break;
            case 1: return(new Class2()); break;
            // for any new class that you add you must add a case entry here
        }
    }
}

/*
Your Program
*/
import mypackage.InterfaceForAllMyClasses;
import mypackage.AbstractFactory;
MyProgram.as
public class MyProgram
{
    var abstractFactory:AbstractFactory = new AbstractFactory();

    public function main()
    {
        var x:InterfaceForAllMyClasses=AbstractFactory.giveMeObject(0);
        var y:InterfaceForAllMyClasses=AbstractFactory.giveMeObject(1);

        x.callMe();
        x.callMe2();
        y.callMe();
        y.callMe2();        
    }
}

外部モジュール(swfs)で宣言されているためにメインアプリケーションからクラスをインポートできない場合は、各モジュールを抽象ファクトリとして作成できます。例を次に示します。

Interfaces/IModuleInterface.as

package Interfaces
    {
        public interface IModuleInterface
        {
            function giveMeObject(classNumber:Number):IObjectInterface;
        }
    }

Interfaces/IObjectInterface.as

package Interfaces
    {
        public interface IObjectInterface
        {
            function callMe():void;
            function callMeToo():void;
        }
    }

Modules/ModuleOne.mxml

    <?xml version="1.0" encoding="utf-8"?>
    <s:Module xmlns:fx="http://ns.adobe.com/mxml/2009" 
      xmlns:s="library://ns.adobe.com/flex/spark" 
      xmlns:mx="library://ns.adobe.com/flex/mx" width="400" height="300"         implements="Interfaces.IModuleInterface">
        <fx:Script>
            <![CDATA[
                import Interfaces.IObjectInterface;
                public function giveMeObject(classNumber:Number):IObjectInterface
                {
                    switch(classNumber)
                    {
                        case 1:
                            trace("ModuleOne: Instantiating 1");
                            return(new ModuleOneClassOne()); 
                            break;
                        case 2:
                            trace("ModuleOne: Instantiating 2");
                            return(new ModuleOneClassTwo()); 
                            break;
                    }
                    return(null);
                }
            ]]>
        </fx:Script>
        <fx:Declarations>
            <!-- Place non-visual elements (e.g., services, value objects) here -->
        </fx:Declarations>
        <s:Label x="10" y="10" text="Module One Loaded"/>
    </s:Module>

Modules/ModuleOneClassOne.as

    package Modules
    {
        import Interfaces.IObjectInterface;

        public class ModuleOneClassOne implements IObjectInterface
        {
            public function ModuleOneClassOne()
            {
                trace("ModuleOneClassOne: Instantiated");
            }

            public function callMe():void
            {
                trace("ModuleOneClassOne: called callMe()");
            }

            public function callMeToo():void
            {
                trace("ModuleOneClassOne: called callMeToo()");
            }
        }
    }

Modules/ModuleOneClassTwo.as

    package Modules
    {
        import Interfaces.IObjectInterface;

        public class ModuleOneClassTwo implements IObjectInterface
        {
            public function ModuleOneClassTwo()
            {
                trace("ModuleOneClassTwo: Instantiated");
            }

            public function callMe():void
            {
                trace("ModuleOneClassTwo: called callMe()");
            }

            public function callMeToo():void
            {
                trace("ModuleOneClassTwo: called callMeToo()");
            }
        }
    }

AbstractFactoryInModules.mxml

    <?xml version="1.0" encoding="utf-8"?>
    <s:WindowedApplication xmlns:fx="http://ns.adobe.com/mxml/2009"
                   xmlns:s="library://ns.adobe.com/flex/spark"
                   xmlns:mx="library://ns.adobe.com/flex/mx"
                   width="345" height="200">

        <fx:Script>
            <![CDATA[
                import Interfaces.IModuleInterface;
                import Interfaces.IObjectInterface;

                import mx.events.ModuleEvent;
                protected var module:IModuleInterface;
                protected var object:IObjectInterface;

                protected function ButtonLoadSwf_clickHandler(event:MouseEvent):void
                {
                    loader.unloadModule();
                    loader.url=moduleUrl.text;
                }

                protected function loader_readyHandler(event:ModuleEvent):void
                {
                    this.module = (loader.child) as IModuleInterface;
                }

                protected function ButtonCreateObject_clickHandler(event:MouseEvent):void
                {
                    this.object = this.module.giveMeObject(Number(TClassNumber.text));
                }

                protected function BCallMe_clickHandler(event:MouseEvent):void
                {
                    this.object.callMe();
                }

                protected function BCallMeToo_clickHandler(event:MouseEvent):void
                {
                    this.object.callMeToo();
                }
            ]]>
        </fx:Script>

        <fx:Declarations>
            <!-- Place non-visual elements (e.g., services, value objects) here -->
        </fx:Declarations>
        <s:Button id="ButtonLoadSwf" x="236" y="10" width="99" label="Load SWF"
                  click="ButtonLoadSwf_clickHandler(event)"/>
        <s:Button id="ButtonCreateObject" x="150" y="108" label="Create Object"
                  click="ButtonCreateObject_clickHandler(event)"/>
        <s:TextInput id="TClassNumber" x="96" y="107" width="46" text="1"/>
        <s:ModuleLoader x="10" y="39" width="325" height="60" id="loader" ready="loader_readyHandler(event)">
        </s:ModuleLoader>
        <s:TextInput id="moduleUrl" x="10" y="10" width="218" text="Modules/ModuleOne.swf"/>
        <s:Button id="BCallMe" x="96" y="137" width="150" label="callMe"
                  click="BCallMe_clickHandler(event)"/>
        <s:Button id="BCallMeToo" x="96" y="166" width="150" label="callMeToo"
                  click="BCallMeToo_clickHandler(event)"/>
    </s:WindowedApplication>
于 2013-01-14T14:03:58.453 に答える