【问题标题】:@override a method, Google Closure Compiler@override 一个方法,谷歌闭包编译器
【发布时间】:2012-10-15 14:04:27
【问题描述】:

我正在尝试覆盖超类的方法并使用 Google Closure Compiler 编译代码,但我收到有关错误类型的警告。

/Users/Jan/dev/cro/public/app/js/LibraryController.js:55: WARNING -
  mismatch of the setState property type and the type of the property it overrides
  from superclass app.Controller
original: function (this:app.Controller, Object): undefined
override: function (this:app.LibraryController, Object, string, string): undefined
app.LibraryController.prototype.setState = function (state, section, article) {

如您所见,我没有更改 super 方法接受的参数类型,也没有更改返回的类型。

有谁知道如何解决这个问题?谢谢。

为了澄清,这里是各个方法的定义。

/**
 * @param {!Object} state The new state.
 */
app.Controller.prototype.setState = function (state) { ... };

/**
 * @param {!Object} state The new state.
 * @param {string} section A section ID.
 * @param {string} article An article ID.
 * @override
 */
app.LibraryController.prototype.setState = function (state, section, article) { ... }

【问题讨论】:

    标签: javascript inheritance google-closure-compiler jsdoc


    【解决方案1】:

    被覆盖的方法必须具有相同(或至少非常相似)的签名。具体来说,子类方法必须能够在任何可以使用基类的地方使用。查看完整讨论:https://groups.google.com/d/topic/closure-compiler-discuss/o_CMZAvFOLU/discussion

    您在上面发布的错误消息表明,覆盖方法具有比原始方法更多的 必需 参数,这是明确不允许的。但是,您可以有更多 可选 参数。

    【讨论】:

    • 谢谢!我没有意识到“子类必须可以替代基类,也就是说,您需要能够在可以使用基类的任何地方使用子类。”。如果我将添加的参数设为可选,一切正常;)
    • 正确 - 我是凭记忆写的。我会更新答案,使其更准确/完整。
    • 我非常喜欢这篇文章:closuretools.blogspot.com/2012/06/…
    猜你喜欢
    • 1970-01-01
    • 2014-06-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多