那么你能帮我理解 String toString() { "$email"}
在下面的程序中会做什么吗???
class User
{
Long id
Long version
String email
String password
String toString()
{ "$email" }
def constraints =
{
email(email:true)
password(blank:false, password:true)
}
}
最佳答案
这意味着当调用 toString()
时,将返回 email
变量中的任何内容。
也可以写成:
@Override
String toString() {
email
}
但作者决定变得“聪明”(是的,我在讽刺!)并使用 $ notation将变量嵌入到字符串中。
备注:
在 groovy 中,您不必使用 return
- 默认行为是返回方法内的最后一条语句。
关于groovy - "String toString() { .. }"是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25030187/