2011-05-08 7 views
0

나는 다음과 같은 시나리오 한 :이 허용되는 것 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. 

    } 

} 

이름에 대한

내가는 무비 클립과 내가 연결을 만들었습니다? 문제를 어떻게 해결할 수 있습니까?

갱신

확인이 정확한 시나리오하지만 난 아직 확인하지 못했습니다 더 간단한 일이다.

원래 시나리오

내가 무비 클립 내가 영화 내부 요소 자체를 클립 수정할 수 있도록하려면 user_bg_generic 클래스 내부에 'MovieClip`

를 확장 user_bg_generic를 확장 user_bg_me라는 이름의 연결을 가지고 user_bg_me라고해야한다는 것입니다. super.element_name을 사용하면 해당 등록 정보를 찾을 수 없다는 오류가 발생합니다.

어떤 아이디어가 있습니까?

+0

그러나'C' 클래스는'MovieClip' 클래스의 서브 클래스이므로'A' 클래스와 아무런 관련이 없습니다. 'A'는'MovieClip'을 확장하지 않고'B'는'A'를 확장하고'C'는'B'를 확장합니까? – Taurayi

+0

welp 나는이 예제가 정확히 무엇을 달성하려했는지 설명하기에 충분하지 않다고 생각한다. 정확한 시나리오로 메인 포스트를 업데이트했습니다. – ufk

+1

용어가 약간 모호합니다. 즉, 연결로 인해 항목이 확장되지 않고 의미가 명확하지 않은 경우 FLA를 보는 데 도움이됩니다. 그건 제쳐두고, 당신은 여전히 ​​슈퍼에 대해 혼란스러워합니다. 예제에서 super.element_name은 MovieClip 클래스의 "element_name"속성을 찾으려고합니다. 그러나 user_bg_generic 클래스가 저작 도구에서 만든 MovieClip 내부의 요소에 대한 참조를 가져오고 user_bg_generic의 하위 클래스로 내보내도록 설정하면 this.element_name 또는 this [ "element_name "] 형식 표기법이 아니라 '수퍼'표기법. –

답변

2

나는 당신이 상속이 작동하는 방법에 대해 혼란 스러울 수 있다고 생각합니다. 특히 부모 클래스와 하위 클래스 간의 관계.

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

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

우리는 지금을 만들 수 있습니다

부모 클래스는 아이 서브 클래스가 행동하는의, here'a 매우 간단한 예제의 사용을 할 수 메서드와 속성의 형태로 행동에 노출 될 수 있습니다 부모의 눈에 보이는 행동 (함수와 속성)를 얻을 것이 부모 클래스의 자식 클래스 : 이제

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!"); 
    } 
} 

, 자식 클래스는 부모의 기능과 속성에 액세스 할 수 있습니다 관계, 확장 한 클래스는 한 가지 방법으로 만 작동합니다. 의 우리 방법을 살펴 보자,

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에 인스턴스에 속하는 텍스트 필드에 액세스하려고의 문제를 해결할 수 :

// 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가 있는지 확인하면됩니다.

희망이 있습니다.

+0

을 사용할 필요가 있습니다! 내가 생각할 수있는 모든 질문을 다룹니다. – ufk

2

계층 구조가 거꾸로 있습니다. 클래스 C는 A의 멤버에 액세스 할 수 없습니다. A는 C의 하위 클래스이므로 다른 방법은 아닙니다. C 클래스의 "super.super"는 MovieClip이 확장하는 Sprite 클래스를 참조하기 때문에 작동하더라도 원하는 것을 얻지 못합니다.

스프 -> 무비 클립 -> C -> B -> A가 더 중요하지만

계층 구조

, 당신은 일반적으로 슈퍼 클래스 멤버에 액세스 할 수 슈퍼 키워드가 필요하지 않습니다.수퍼 클래스 멤버를 동일한 이름의 하위 클래스 멤버로 재정의하고 차별화해야 할 때만 키워드가 필요합니다. 명명 충돌이없는 경우 수퍼 또는 클래스 이름 한정자 없이는 수퍼 클래스의 공개 및 보호 된 멤버에 액세스 할 수 있습니다. 정말로 super.super가 필요하다고 생각한다면 상속 디자인을 재고해야합니다.

관련 문제