是否允许更改非静态类型语言中的方法签名
假设性和学术性问题。
伪代码:
<pre><code>
class Book{
read(theReader)
}
class BookWithMemory extends Book {
read(theReader, aTimestamp = null)
}
</pre></code>
假设:
接口(如果支持)将禁止它
支持参数的默认值
注意:
- PHP 会为此触发严格的标准错误。
Hypothetic and academic question.
pseudo-code:
<pre><code>
class Book{
read(theReader)
}
class BookWithMemory extends Book {
read(theReader, aTimestamp = null)
}
</pre></code>
Assuming:
an interface (if supported) would prohibit it
default value for parameters are supported
Notes:
- PHP triggers an strict standards error for this.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
对于 PHP 严格模式抱怨这种覆盖,我并不感到惊讶。很容易无意中出现类似的情况,其中类层次结构的一部分被编辑为使用新签名,并且一个或几个类不同步。
为了避免歧义,请将新方法命名为不同的名称(对于本例,可能是
readAt
?),并覆盖read
以在新班级。这使得解释器以及阅读代码的任何人都清楚其意图。这种情况下的实际行为是依赖于语言的——更具体地说,它取决于方法选择器的签名数量以及参数的传递方式。
如果名称本身就是选择器(如在 PHP 或 Perl 中),那么这取决于语言如何处理不匹配的方法参数列表。如果默认参数是在调用站点根据接收者的静态类型而不是在被调用者的入口点处理的,那么当通过基类引用调用时,您最终会得到一个未定义的参数值,而不是指定的默认值,类似于如果没有指定默认值会发生什么。
如果参数的数量(带或不带类型)是方法选择器的一部分(如在 Erlang 或 E 中),这在 JVM 或 CLR 上运行的动态语言中很常见,那么您有两种不同的方法。创建一个带有附加参数的新重载,并使用使用默认参数值调用新重载的方法重写基本方法。
I'm not surprised that PHP strict mode complains about such an override. It's very easy for a similar situation to arise unintentionally in which part of a class hierarchy was edited to use a new signature and a one or a few classes have fallen out of sync.
To avoid the ambiguity, name the new method something different (for this example, maybe
readAt
?), and overrideread
to callreadAt
in the new class. This makes the intent plain to the interpreter as well as anyone reading the code.The actual behavior in such a case is language-dependent -- more specifically, it depends on how much of the signature makes up the method selector, and how parameters are passed.
If the name alone is the selector (as in PHP or Perl), then it's down to how the language handles mismatched method parameter lists. If default arguments are processed at the call site based on the static type of the receiver instead of at the callee's entry point, when called through a base class reference you'd end up with an undefined argument value instead of your specified default, similarly to what would happen if there was no default specified.
If the number of parameters (with or without their types) are part of the method selector (as in Erlang or E), as is common in dynamic languages that run on JVM or CLR, you have two different methods. Create a new overload taking additional arguments, and override the base method with one that calls the new overload with default argument values.
如果我正确地阅读了这个问题,这个问题似乎非常特定于语言(因为它不适用于所有动态语言),因为我知道你可以在 ruby 中做到这一点。
除了 ruby 之外,我不确定还有哪些动态语言。这似乎也是一个非常主观的问题,因为在这个问题的两边至少设计了两种语言(方法重载与非方法重载:ruby 与 php)。
If I am reading the question correctly, this question seems very language specific (as in it is not applicable to all dynamic languages), as I know you can do this in ruby.
I am not sure about dynamic languages besides ruby. This seems like a pretty subjective question as well, as at least two languages were designed on either side of the issue (method overloading vs not: ruby vs php).