超分配在AS3变量 [英] Assign super to variable in AS3

查看:121
本文介绍了超分配在AS3变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这样的:

public class Base {
    public function whoAmI() {
        trace("base");
    }
}

public class Extended extends Base {

    public function Extended() {
        this.whoAmI() //prints extended
        super.whoAmI() //prints base

        var test = super;
        test.whoAmI() //prints extended
    }

    public override function whoAmI() {
        trace("extended");
    }
}

现在的问题是,当我做 VAR测试=超,好像被分配到测试,而不是

The problem is when I do var test = super, it seems like this is assigned to test instead of super.

是否有可能做了分配,以便 test.whoAmI()打印基地?

Is it possible to do the assignment so that test.whoAmI() prints "base"?

修改:在注释它被说的方式使用超我建议将打破覆盖。我不认为是这样。的方式,我想起来了,可以用同样的方法。据我所知,是不是超级的实现方式,但使用这种方式不会打破压倒一切的人都声称。因此,例如,以相同的方式,这是可能的:

Edit: In the comments it is being said that using super in the way I propose would break overriding. I don't think that's the case. The way I am thinking of it, super could be used the same way as this. I understand that is not the way super is implemented, but using it that way would not break overriding as people are claiming. So for example the same way this is possible:

var test = this;
test.whoAmI();

这应该是可能的:

var test = super;
super.whoAmI();

这显然是语言实现者不能做的事情这样的选择,我不明白为什么。它不破的东西,但我想它确实让他们更加复杂。

It is obviously the choice of the language implementer to not do things this way, and I don't understand the reason why. It doesn't break things, but I guess it does make them more complicated.

我的不可以提示型铸造类。显然,这是行不通的。

I am not suggesting type-casting this to the super class. Obviously that wouldn't work.

推荐答案

您所想的本和超级为2个不同的情况下,2个不同的东西,但他们实际上是指向同一个对象(明显),因此在最终它总是本。使用超级只是一个特殊的关键字,它允许实例指向overrided定义了继承链,它并不指向不同的对象。因此,超级做正确的工作,它指向的实例,并允许你每次使用它来访问overrided定义的时间,仅此而已。有当然的,就是要存储的关键字在一个变量没有任何意义,因为在这种情况下,它只是返回正确指向的实例这始终是本。 这简直就是被误解的继承原则的情况下,我已经看到过,超被误认为是某种形式的实例的包装器周围的物体本,而它实际上总是同一个对象。

You are thinking of "this" and "super" as 2 different instances, 2 different things but they in fact point to the same object (obviously) so at the end it's always "this". Using super is just a special keyword that allows the instance to point to the overrided definitions up the inheritance chain, it does not point to a different object. So "super" does correctly its job, it points to the instance and allow you each time you use it to access overrided definitions and that's it. There's of course no point on trying to store that keyword in a variable since in that case it just return correctly the instance it points to which is always "this". It's simply a case of misunderstood inheritance principle and I've seen it before, super is mistaken for some kind of instance wrapper up the inheriatnce chain around the object "this" while it's in fact and always the same object.

这篇关于超分配在AS3变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆