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

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

问题描述

为什么这会在Flash Builder中产生一个错误:
$ b

package {
public class Foo {
override public function toString():String {
returnFoo;





标签填写表明这是可用于重写...



错误消息:

 在这一行的多个标记:
-public
-1020:方法标记的覆盖必须覆盖另一个方法。
-overridesObject.toString


解决方案

c $ c $>重写 toString()方法上。

流行的对 toString()方法的误解,即:如果想要提供一个超类方法的自定义实现,覆盖关键字是需要的。但在 Object 的情况下, toString()是动态的,并且在运行时被附加,否定了重载的需要。相反,实现是由开发人员提供的,所以不会在运行时创建。一个只需要写自己的 toString():String 实现。


Why does this produce an error in Flash Builder?:

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

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

Error message:

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

解决方案

Remove override on the toString() method.

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天全站免登陆