0

次のシナリオがあります。

私はMovieClipを持っていて、Aという名前のリンクを作成しました。

class A extends B{

}

class B extends C {
}

class C extends MovieClip {

  public function C() {
  // from the constructor of C i want to be able to reach the elements in Class a.
  // for example I have some text elements that I want to change.
  //  super.super is not allowed. 

  }

}

何が許可されていますか?どうすれば問題を解決できますか?

アップデート

わかりました、これは正確なシナリオですが、私がまだ解決できなかったより単純なシナリオです。

元のシナリオは、「MovieClip」を拡張する拡張user_bg_meという名前のリンケージを持つと呼ばれるMovieClipがあるというものです。user_bg_meuser_bg_generic

クラス内user_bg_genericでは、ムービークリップ自体の内部の要素を変更できるようにしたいと思います。super.element_nameを使用すると、プロパティが見つからないというエラーが発生します。

何か案は?

4

2 に答える 2

2

継承がどのように機能するかについて混乱しているかもしれません。特に、親クラスと子クラスの関係。

親クラスは、子サブクラスが利用できるメソッドとプロパティの形で動作を公開できます。これは、実際の動作の非常に簡単な例です。

public class Parent {
    // Constructor for the Parent Class.
    public function Parent() {
    }

    protected function sayHello() : void {
        trace("Hello!");
    }
}

これで、このParentクラスの子クラスを作成して、Parentの表示可能な動作(関数とプロパティ)を取得できます。

public class Child extends Parent {
    // Constructor for the Child Class.
    public function Child() {
        // Call the 'parent' classes constructor.
        super();

        // This child class does not define a function called "sayHello" but
        // the Parent class which this Child class extends does, and as a result
        // we can make a call to it here.  This is an example of how the child Class
        // gains the behaviours of the Parent class.
        this.sayHello();
    }

    public function sayGoodbye() : void {
        trace("Goodbye!");
    }
}

現在、子クラスが拡張する親クラスの関数とプロパティにアクセスできるこの関係は、一方向にしか機能しません。つまり、Parentクラスには、その子が宣言することを選択できる関数とプロパティについての知識がないため、次のコードは機能しません。

public class Parent {
    public function Parent() {
        // Trying to call the sayGoodbye() method will cause a compilation Error.
        // Although the "sayGoodbye" method was declared in the Child class, the
        // Parent Class has no knowledge of it.
        this.sayGoodbye();
    }
}

次に、親クラスからFLAのインスタンスに属するTextFieldにアクセスしようとする問題をどのように解決できるかを見てみましょう。

// it is important to note that by extending the MovieClip class, this Parent Class now
// has access to all the behaviours defined by MovieClip, such as getChildByName().
public class Parent extends MovieClip {
    // This is a TextField that we are going to use.
    private var txtName : TextField;

    public function Parent() {
        // Here we are retrieving a TextField that has an instance name of txtName
        // from the DisplayList.  Although this Parent Class does not have  a TextField
        // with such an instance name, we expect the children that extend it to declare
        // one.
        txtName = this.getChildByName("txtName") as TextField;

        // Check to see if the Child class did have an TextField instance called 
        //txtName.  If it did not, we will throw an Error as we can not continue.
        if (txtName == null) {
            throw new Error("You must have a TextField with an instance name of 'txtName' for this Parent Class to use.");
        }

        // Now we can make use of this TextField in the Parent Class.
        txtName.text = "Hi my name is Jonny!";
    }
}

これで、FLAに、この親クラスを拡張するリンケージと同じ数のインスタンスを含めることができます。親クラスがその処理を実行できるように、インスタンス名が「txtName」のTextFieldがあることを確認する必要があります。

お役に立てれば :)

于 2011-05-08T15:44:41.670 に答える
2

階層が逆になっています。AはCのサブクラスであり、その逆ではないため、クラスCはAのメンバーにアクセスできません。Cクラス内の「super.super」は、MovieClipが拡張するSpriteクラスを参照するため、機能したとしても目的の結果が得られません。

スプライト->ムービークリップ->C->B->Aは階層です

ただし、さらに重要なのは、通常、スーパークラスのメンバーにアクセスするためにsuperキーワードは必要ないということです。同じ名前のサブクラスメンバーでスーパークラスメンバーをオーバーライドし、区別する必要がある場合にのみ、キーワードが必要になります。名前の競合がない場合は、スーパー名またはクラス名の修飾子がなくても、スーパークラスのパブリックメンバーと保護されたメンバーにアクセスできます。本当にsuper.superが必要だと思う場合は、継承の設計を再検討する必要があります。

于 2011-05-08T16:13:54.820 に答える