2011-05-08 16 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_me,有一個名爲user_bg_me聯動擴展user_bg_generic擴展「MovieClip`

user_bg_generic類我希望能夠修改影片中的元素剪輯本身。使用super.element_name會提供該屬性未找到的錯誤。

有什麼想法?

+0

但是'C'類是'MovieClip'類的子類,所以它與'A'類沒有聯繫。你確定'A'不擴展'MovieClip',然後'B'擴展'A',然後'C'擴展'B'? – Taurayi 2011-05-08 14:37:21

+0

welp我想這個例子不足以解釋我試圖達到的目標。我用確切的場景更新了主要帖子。 – ufk 2011-05-08 17:08:56

+1

你的術語有點含糊不清(即一個連接不能擴展事物,不確定你的意思),這將有助於看到FLA。除此之外,你仍然對超級感到困惑。示例中的super.element_name將嘗試查找類MovieClip的屬性「element_name」。但它聽起來像是你想要的是爲user_bg_generic類獲取對創作工具中創建的MovieClip中的內容的引用,並將其設置爲導出爲user_bg_generic的子類,爲此您需要this.element_name或this [「element_name 「]類型符號,而不是」超級「。 – 2011-05-08 18:40:41

回答

2

我想你可能會對繼承的工作方式感到困惑;特別是父母和兒童班級之間的關係。

父類可以在方法和屬性的形狀暴露行爲,孩子子類可以使用,here'a很簡單的,在行動例子:

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

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

現在,我們可以創建一個子類此父類,將獲得家長可見的行爲(功能和性能):

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 2011-05-11 09:03:46

2

你已經得到你的層次結構向後。 C類不能訪問A的成員,因爲A是C的子類,而不是相反。在C類中的「super.super」不會讓你得到你想要的,即使它工作,因爲它會引用MovieClip擴展的Sprite類。

雪碧 - >影片剪輯 - 「ç - >乙 - >是您的層次結構

更重要的是,你通常不會需要超級關鍵字來訪問父類成員。只有在使用具有相同名稱的子類成員重寫超類成員並且需要區分時,才需要關鍵字。如果沒有任何命名衝突,則可以訪問超類的公共和受保護成員,而不使用super或類名限定符。如果你真的認爲你需要super.super,你需要重新考慮你的繼承設計。