@override一个方法,Google Closure Compiler

3
我正在尝试覆盖超类的一个方法并使用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) {

正如您所看到的,我没有改变超级方法接受的参数类型,也没有改变返回类型。

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

为了澄清,以下是各个方法的定义。

/**
 * @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) { ... }
1个回答

4
重写方法必须具有相同(或至少非常相似)的签名。具体而言,子类方法必须能够在基类可以使用的任何地方使用。请参见完整讨论:https://groups.google.com/d/topic/closure-compiler-discuss/o_CMZAvFOLU/discussion 您上面发布的错误消息显示覆盖方法比原始方法具有更多必需的参数,这是不允许的。但是,您可以有更多的可选的参数。

谢谢!我没有意识到“子类必须能够替代基类,也就是说,你需要能够在任何可以使用基类的地方使用子类。”如果我将添加的参数设置为可选,一切都没问题;) - J. K.
正确 - 我是凭记忆写的。我会更新答案,使其更加准确/完整。 - Chad Killingsworth
1
我真的很喜欢这篇文章:http://closuretools.blogspot.com/2012/06/subtyping-functions-without-poking-your.html - John

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接