Я думаю, вы можете запутаться из-за того, как работает наследование;в частности, отношения между родительским и дочерним классами.
Родительский класс может представлять поведение в форме методов и свойств, которые могут использовать подклассы child , вот очень простой примерчто в действии:
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();
}
}
Теперь давайте посмотрим накак мы могли бы решить вашу проблему, пытаясь получить доступ к TextField, который принадлежит экземпляру в 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, сколькорасширить этот родительский класс.Вам просто нужно убедиться, что у них есть TextField с именем экземпляра 'txtName', чтобы родительский класс мог делать свое дело.
Надеюсь, это поможет:)