【问题标题】:Can you call a parent method that has been overridden in Haxe?你能调用在 Haxe 中被覆盖的父方法吗?
【发布时间】:2015-12-05 23:44:48
【问题描述】:

Haxe 允许子构造函数类使用 super() 调用父构造函数,但在构造函数方法之外绑定使用 super() 会触发错误。如果父方法已被覆盖,孩子是否可以调用父方法?

匆忙写的例子:

class Parent {
    var thing:Bool;

    public function someFunc(){
        if(this.thing){
           return "TRUE!";
        } else {
           return "FALSE!";
        }
    }
}

class Child extends Parent {

    var thing2:Bool;

    public override function someFunc() {
        if(this.thing2){
            return "TRUE!";
        } else {
            return someFunc(); //call to parent function? 
        }
    }
}

【问题讨论】:

  • 也许我说的是显而易见的,而不是返回字符串“TRUE!”您还可以在 Haxe 中使用真正的布尔值:truefalse
  • 这只是一个愚蠢的例子:P

标签: haxe


【解决方案1】:

是的,这可以通过 super 关键字来实现。

public override function someFunc() {
    if (this.isWorking) {
        return true;
    } else {
        return super.someFunc(); 
    }
}

http://haxe.org/manual/types-class-inheritance.html

【讨论】:

    【解决方案2】:

    child 中的 super.someFunc() 就是你想要的。

    (super() 总是表示父类的构造函数,只能从子类的构造函数中调用)

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-12-13
      • 1970-01-01
      • 2011-01-28
      • 2010-10-18
      • 2020-09-20
      • 2011-11-10
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多