开发者

how can I reach text elements of a super class ?

I have the following scenario:

i have a MovieClip and I created a linkage for it named 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. 

  }

}

what is allowed? how can I resolve the issue ?

update

ok this is an exact scenario but a more simple one that I still wasn't able to resolve.

The original scenario is that I have a MovieClip called user_bg_me that has a linkage named user_bg_me that extends user_bg_genericthat extends 'MovieClip`

inside user_bg_generic class i want to be able to modify elements inside the movie clip itself. using super.e开发者_C百科lement_name provides an error that the property isn't found.

any ideas?


I think you may be getting confused over how inheritance works; in particular the relationship between Parent and Child classes.

A Parent class can expose behaviours in the shape of methods and properties that child subclasses can make use of, here'a very simple example of that in action:

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

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

We can now create a child class of this Parent class which will gain the Parent's visible behaviours (functions and properties):

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

Now, this relationship, where Child classes can access the function and properties of the Parent classes they extend only works one way. That is to say, a Parent class has no knowledge of the functions and properties that its children may choose to declare, so as a result, the following code will not work:

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();
    }
}

Now, let's look at how we could possibly solve your problem of trying to access a TextField which belongs to an Instance in the FLA from the Parent Class:

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

You can now have as many Instances in your FLA which, which Linkage, extend this Parent Class. You just have to ensure that they have a TextField with the instance name 'txtName' so that the Parent Class can do its thing.

Hope this helps :)


You've got your hierarchy backwards. Class C can't access members of A because A is a subclass of C, not the other way around. "super.super" from within the C class wouldn't get you what you want even if it worked because it would refer to the Sprite class that MovieClip extends.

Sprite -> MovieClip -> C -> B -> A is your hierarchy

More importantly though, you don't normally need the super keyword to access superclass members. You only need the keyword when you've overridden superclass members with subclass members of the same name and you need to differentiate. If there aren't any naming conflicts, you can access public and protected members of the superclass without the super or class name qualifier. If you really think you need super.super, you need to reconsider your inheritance design.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜