覆盖 Object.toString 错误 [英] Override Object.toString Error

查看:26
本文介绍了覆盖 Object.toString 错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为什么这会在 Flash Builder 中产生错误?:

Why does this produce an error in Flash Builder?:

package {
  public class Foo {
    override public function toString():String {
      return "Foo";
    }
  }
}

Tab 补全表明这可用于覆盖...

Tab completion suggests that this is available for override...

错误信息:

Multiple markers at this line:
-public
-1020: Method marked override must override another method.
-overridesObject.toString

推荐答案

删除 toString() 方法上的 override.

关于toString() 方法有一个普遍的误解,即:如果要提供超类方法的自定义实现,override 关键字是需要.但是在 Object 的情况下,toString() 是动态的并且在运行时附加,不需要覆盖.相反,实现将由开发人员提供,因此不会在运行时创建.只需编写自己的 toString():String 实现.

There is a popular misconception among about the toString() method, namely: if one wants to provide a custom implementation of a super class method, the override keyword is needed. But in case of Object, toString() is dynamic and is attached at runtime, negating the need for overriding. Instead, the implementation is to be provided by the developer so one is not created at runtime. One just needs to write their own toString():String implementation.

这篇关于覆盖 Object.toString 错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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