加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 大数据 > 正文

groovy – 具有命名参数的TupleConstructor

发布时间:2020-12-14 16:24:32 所属栏目:大数据 来源:网络整理
导读:我有这个脚本: def person = new Person(lastName: "foo",firstName: "bar")println person@groovy.transform.TupleConstructor @groovy.transform.ToString(includeNames = true,includeFields=true) class Person{ def lastName def firstName } 为什么它
我有这个脚本:

def person = new Person(lastName: "foo",firstName: "bar")

println person

@groovy.transform.TupleConstructor  
@groovy.transform.ToString(includeNames = true,includeFields=true)  
class Person
{  
   def lastName  
   def firstName  
}

为什么它给了我:

Person(lastName:[lastName:foo,firstName:bar],firstName:null)

为什么firstName保持为null并且lastName具有参数的映射?

我正在使用groovy 1.8.6

解决方法

因为您将字段声明为def,所以TupleConstructor实际上添加了2个构造函数:

Person( Object first name )

Person( Object firstName,Object secondName )

然后,当您调用地图样式构造函数时,它调用一个arg元组构造函数,并将map作为参数.

向字段添加类型应该可以解决此问题

正如它在docs for TupleConstructor中所说:

Limitations: – Groovy’s normal map-style naming conventions will not be available if the first property (or field) has type LinkedHashMap or if there is a single Map,AbsotractMap or HashMap property (or field)

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读