我想你可能會對繼承的工作方式感到困惑;特別是父母和兒童班級之間的關係。
父類可以在方法和屬性的形狀暴露行爲,孩子子類可以使用,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,以便父類可以完成它的工作。
希望這有助於:)
但是'C'類是'MovieClip'類的子類,所以它與'A'類沒有聯繫。你確定'A'不擴展'MovieClip',然後'B'擴展'A',然後'C'擴展'B'? – Taurayi 2011-05-08 14:37:21
welp我想這個例子不足以解釋我試圖達到的目標。我用確切的場景更新了主要帖子。 – ufk 2011-05-08 17:08:56
你的術語有點含糊不清(即一個連接不能擴展事物,不確定你的意思),這將有助於看到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