如何在Groovy中设置新属性并提供其setter

如何在Groovy中设置新属性并提供其setter,groovy,closures,metaprogramming,Groovy,Closures,Metaprogramming,我有一个groovy类: class Car { int speed = 0 } 我想使用元编程引入一个新属性“color”,并为Car对象的实例提供setColor方法,如下所示: def c = new Car() c.metaClass.setProperty("color", "red") c.metaClass.setColor = { def newColor-> "color switched from $existingColor to

我有一个groovy类:

class Car {
    int speed = 0
}
我想使用元编程引入一个新属性“color”,并为Car对象的实例提供setColor方法,如下所示:

 def c = new Car()

 c.metaClass.setProperty("color", "red")

 c.metaClass.setColor = { 
       def newColor-> "color switched from $existingColor to $newColor
 }
c.metaClass.color << { col ->
    println "color switched from $delegate.color to $col"
    c.color = col
}

c.color('yellow') // prints "color switched from blue to yellow"
assert c.color == 'yellow'
我的最终目标是,当我打电话时:

c.color("yellow")
它打印出:

color switched from red to yellow"
我已经用上面的代码处理了c.color部分,但没有第二部分(setColor)

有人能帮我做到这一点,或者告诉我这是否可能


谢谢。

当您添加属性时,您可以免费获得getter和setter,例如

class Car {
    int speed = 0
}
def c = new Car()
c.metaClass.setProperty("color", "red")
assert c.color == 'red'
c.setColor('blue')
assert c.getColor() == 'blue'
如果最终目标是调用名为
color
的方法来设置
color
属性,则可以添加如下内容:

 def c = new Car()

 c.metaClass.setProperty("color", "red")

 c.metaClass.setColor = { 
       def newColor-> "color switched from $existingColor to $newColor
 }
c.metaClass.color << { col ->
    println "color switched from $delegate.color to $col"
    c.color = col
}

c.color('yellow') // prints "color switched from blue to yellow"
assert c.color == 'yellow'
c.metaClass.color
println“颜色从$delegate.color切换到$col”
c、 颜色=颜色
}
c、 颜色('yellow')//打印“颜色从蓝色切换到黄色”
断言c.color==“黄色”

添加属性时,您可以免费获得getter和setter,例如

class Car {
    int speed = 0
}
def c = new Car()
c.metaClass.setProperty("color", "red")
assert c.color == 'red'
c.setColor('blue')
assert c.getColor() == 'blue'
如果最终目标是调用名为
color
的方法来设置
color
属性,则可以添加如下内容:

 def c = new Car()

 c.metaClass.setProperty("color", "red")

 c.metaClass.setColor = { 
       def newColor-> "color switched from $existingColor to $newColor
 }
c.metaClass.color << { col ->
    println "color switched from $delegate.color to $col"
    c.color = col
}

c.color('yellow') // prints "color switched from blue to yellow"
assert c.color == 'yellow'
c.metaClass.color
println“颜色从$delegate.color切换到$col”
c、 颜色=颜色
}
c、 颜色('yellow')//打印“颜色从蓝色切换到黄色”
断言c.color==“黄色”

您已经非常接近让它工作了。由于您创建了一个具有setter的新属性,因此只需执行以下操作:

 c.metaClass.getColor = {'red'}

 c.metaClass.setColor = { 
       def newColor-> println "color switched from ${delegate.color} to $newColor"
 }

 c.color = "yellow"
请注意,这并不会生成一个已设置的属性,而只是提供了一种机制来为代理颜色注入get和set方法

这样做可能会解决这个问题:

def currentColor = 'red'
def previousColor = '' 
c.metaClass.getColor = { currentColor }
c.metaClass.getPreviousColor = { previousColor }

c.metaClass.setColor = { 
   def newColor-> previousColor = delegate.color; currentColor = newColor
}
c.color = "yellow"
println "Changed color from $c.previousColor to $c.color"
c.color = "blue"
println "Changed color from $c.previousColor to $c.color"

但是现在我们进入的代码纯粹是为了实验而不是为了生产:)

您已经非常接近让它工作了。由于您创建了一个具有setter的新属性,因此只需执行以下操作:

 c.metaClass.getColor = {'red'}

 c.metaClass.setColor = { 
       def newColor-> println "color switched from ${delegate.color} to $newColor"
 }

 c.color = "yellow"
请注意,这并不会生成一个已设置的属性,而只是提供了一种机制来为代理颜色注入get和set方法

这样做可能会解决这个问题:

def currentColor = 'red'
def previousColor = '' 
c.metaClass.getColor = { currentColor }
c.metaClass.getPreviousColor = { previousColor }

c.metaClass.setColor = { 
   def newColor-> previousColor = delegate.color; currentColor = newColor
}
c.color = "yellow"
println "Changed color from $c.previousColor to $c.color"
c.color = "blue"
println "Changed color from $c.previousColor to $c.color"
但现在我们进入了纯粹用于实验而非生产的代码:)