我怎样才能到达超类的文本元素?

发布于 2024-11-05 10:27:14 字数 703 浏览 3 评论 0原文

我有以下场景:

我有一个 MovieClip,我为其创建了一个名为 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 的 MovieClip,它有一个名为 user_bg_me 的链接,该链接扩展了 user_bg_generic,该链接扩展了

内部的 “MovieClip” user_bg_generic 类我希望能够修改影片剪辑本身内的元素。使用 super.element_name 会出现找不到该属性的错误。

有什么想法吗?

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.element_name provides an error that the property isn't found.

any ideas?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

哀由 2024-11-12 10:27:14

我认为您可能对继承的工作原理感到困惑;特别是父类和子类之间的关系。

父类可以以子类可以使用的方法和属性的形式公开行为,这是一个非常简单的实际示例:

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 中实例的 TextField:

// 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,以便父类可以执行其操作。

希望这有帮助:)

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 :)

日暮斜阳 2024-11-12 10:27:14

你的层次结构倒退了。 C 类无法访问 A 的成员,因为 A 是 C 的子类,而不是相反。 C 类中的“super.super”即使有效也不会得到您想要的,因为它会引用 MovieClip 扩展的 Sprite 类。

雪碧 ->影片剪辑 -> C-> B-> A 是您的层次结构,

但更重要的是,您通常不需要 super 关键字来访问超类成员。仅当您使用同名的子类成员覆盖超类成员并且需要区分时,才需要关键字。如果不存在任何命名冲突,则无需使用 super 或类名限定符即可访问超类的公共和受保护成员。如果你真的认为你需要 super.super,你需要重新考虑你的继承设计。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文