在groovy中使用默认值命名参数 [英] named parameters with default values in groovy

查看:1244
本文介绍了在groovy中使用默认值命名参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以在groovy中使用默认值命名参数?我的计划是制作一种对象工厂,为了获得具有默认值的对象,可以根本不调用任何参数。此外,我需要的功能显式设置对象的任何参数。例如,我相信这可以通过Python关键字参数实现。



我现在尝试使用的代码如下所示

  //工厂方法
def createFoo(name ='John Doe',年龄= 51,地址='High Street 11'){
返回新的Foo(名称,年龄,地址)
}

//调用
Foo foo1 = createFoo()//用默认值创建Foo
Foo foo2 = createFoo(age:21)//创建Foo,其中的年龄参数与defaut不同
Foo foo3 = createFoo(name:'Jane',地址:'Low Street 11')//获取图片
/ / +任何其他可用组合

我正在开发的真正的应用程序将有更多的参数,因此需要更多的组合。



谢谢

更新:



我计划的工厂方法用于测试目的。无法真正触及实际的Foo类,特别是它不是默认值。



@dmahapatro和@codelarks下面的answere使用Map作为参数给了我一个好点一个可能的解决方案的想法。我可以用想要的默认值创建一个映射并覆盖所需的值,并将其传递给工厂方法。这可能会完成这项工作,我会继续这样做,除非我有更好的方法。



我目前的做法

  defaults = [name:'john',age:61,address:'High Street'] 

@ToString(includeFields = true,includeNames = true)
class Foo {
//不能碰这个:)
def name =''
def age = 0
def address =''
}
$ b $ def createFoo(Map params){
返回新的Foo(params)
}

println createFoo(默认值)
println createFoo(默认值<< [age:21])$ ​​b $ b println createFoo(默认值<< [name:'Jane',地址:'Low Street'])

注意: leftShift操作(<<)会修改原始地图,上面的示例年龄在最后的方法调用中也是21。在我的情况下,这不是问题,因为每次在setup方法中都可以新建默认值映射。

对你来说默认情况下(地图构造函数)。你不需要工厂方法。这是一个例子

  import groovy.transform.ToString 

@ToString(includeFields = true,includeNames = true)
class Foo {
String name =Default Name
int age = 25
String address =Default Address
}

println new Foo()
println new Foo(name:John Doe)
println new Foo(name:Max Payne,年龄:30)
println new Foo姓名:John Miller,年龄:40,地址:奥马哈海滩)

//打印
Foo(姓名:默认姓名,年龄:25,地址:默认地址)
Foo(姓名:John Doe,年龄:25,地址:默认地址)
Foo(姓名:Max Payne,年龄:30,地址:默认地址)
Foo(姓名:John Miller,年龄:40岁,地址:奥马哈海滩)

更新

@ codelark的占星术:)。如果类不能设置默认值,你可以像

  @ToString(includeFields = true,includeNames = true )
class Bar {
字符串名称
int age
字符串地址
}

def createBar(Map map = [:]){
def defaultMap = [name:'John Doe',年龄:51,地址:'High Street 11']
new Bar(defaultMap }

println createBar()
println createBar(名称:Ethan Hunt)
println createBar(名称:Max Payne,年龄:30)
println createBar(name:约翰米勒,年龄:40,地址:奥马哈海滩)


//打印
栏(名字:John Doe,年龄:51岁,地址:High Street 11 )
Bar(名字:Ethan Hunt,年龄:​​51,地址:High Street 11)
Bar(名字:Max Payne,年龄:30,地址:High Street 11)
Bar :约翰米勒,年龄:40岁,地址:奥马哈海滩)


Is it possible to have named parameters with default values in groovy? My plan is to make a sort of object factory, which can be called with no arguments at all in order to get an object with default values. Also, I'd need the functionality to explicitly set any of the params for the object. I believe this is possible with Python keyword arguments, for example.

The code I'm attempting with right now is something like below

// Factory method
def createFoo( name='John Doe', age=51, address='High Street 11') {
  return new Foo( name, age, address )
}

// Calls
Foo foo1 = createFoo()  // Create Foo with default values
Foo foo2 = createFoo( age:21 )  // Create Foo where age param differs from defaut
Foo foo3 = createFoo( name:'Jane', address:'Low Street 11' )  // You get the picture
// + any other combination available

The real app that I'm working on will have a lot more of parameters and thus a lot more combinations needed.

Thanks

UPDATE:

The factory method I'm planning is for testing purposes. Cannot really touch the actual Foo class and especially not it's default values.

@dmahapatro and @codelarks answere below had a good point in using a Map as a param that gave me an idea of a possible solution. I could create a map with the wanted defaults and override the needed values, and pass that to the factory method. This'll probably do the job and I'll go with that, unless I get a hint of a better approach.

My current approach below

defaults = [ name:'john', age:61, address:'High Street']

@ToString(includeFields = true, includeNames = true)
class Foo {
  // Can't touch this :)
  def name = ''
  def age = 0
  def address = ''
}

def createFoo( Map params ) {
  return new Foo( params )
}

println createFoo( defaults )
println createFoo( defaults << [age:21] )
println createFoo( defaults << [ name:'Jane', address:'Low Street'] )

NOTE: leftShift operation ( << ) modifies the the original map, so in the above example age will be 21 in the last method call as well. In my case, this is not a problem as the defaults map can be created freshly each time in setup method.

解决方案

Groovy does that for you by default (map constructor). You would not need a factory method. Here is an example

import groovy.transform.ToString

@ToString(includeFields = true, includeNames = true)
class Foo{
    String name = "Default Name"
    int age = 25
    String address = "Default Address" 
}

println new Foo()
println new Foo(name: "John Doe")
println new Foo(name: "Max Payne", age: 30)
println new Foo(name: "John Miller", age: 40, address: "Omaha Beach")

//Prints
Foo(name:Default Name, age:25, address:Default Address)
Foo(name:John Doe, age:25, address:Default Address)
Foo(name:Max Payne, age:30, address:Default Address)
Foo(name:John Miller, age:40, address:Omaha Beach)

UPDATE
@codelark's astrology :). In case the class is not accessible to set default values, you can do like

@ToString(includeFields = true, includeNames = true)
class Bar{
    String name
    int age
    String address
}

def createBar(Map map = [:]){
    def defaultMap = [name:'John Doe',age:51,address:'High Street 11']
    new Bar(defaultMap << map)
}

println createBar()
println createBar(name: "Ethan Hunt")
println createBar(name: "Max Payne", age: 30)
println createBar(name: "John Miller", age: 40, address: "Omaha Beach")


//Prints
Bar(name:John Doe, age:51, address:High Street 11)
Bar(name:Ethan Hunt, age:51, address:High Street 11)
Bar(name:Max Payne, age:30, address:High Street 11)
Bar(name:John Miller, age:40, address:Omaha Beach)

这篇关于在groovy中使用默认值命名参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆