【发布时间】:2011-02-21 07:33:23
【问题描述】:
在下面的 Groovy sn-p 中,我尝试替换 hashCode 和 toString 方法
String.metaClass.toString = {-> "override" }
String.metaClass.hashCode = {-> 22 }
但是当我测试它时,只有 hashCode 的替换有效
String s = "foo"
println s.hashCode() // prints 22
println s.toString() // prints "foo"
toString 是不是某种特殊情况(可能出于安全原因)?
【问题讨论】: