Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
393 views
in Technique[技术] by (71.8m points)

monkeypatching - Dynamically add a property or method to an object in groovy

Is it possible to add a property or a method to an object dynamically in Groovy? This is what I have tried so far:

class Greet {
  def name
  Greet(who) { name = who[0].toUpperCase() + [1..-1] }
  def salute() { println "Hello $name!" }
}

g = new Greet('world')  // create object
g.salute()              // Output "Hello World!"

g.bye = { println "Goodbye, $name" }
g.bye()

But I get the following exception:

Hello World!
Caught: groovy.lang.MissingPropertyException: No such property: bye for class: Greet
Possible solutions: name
    at test.run(greet.groovy:11)
question from:https://stackoverflow.com/questions/4781936/dynamically-add-a-property-or-method-to-an-object-in-groovy

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

If you just want to add the bye() method to the single instance g of the class Greet, you need to do:

g.metaClass.bye = { println "Goodbye, $name" }
g.bye()

Otherwise, to add bye() to all instance of Greet (from now on), call

Greet.metaClass.bye = { println "Goodbye, $name" }

But you'd need to do this before you create an instance of the Greet class

Here is a page on the per-instance metaClass

And here is the page on MetaClasses in general


Also, there's a bug in your constructor. You're missing who from infront of your [1..-1] and if the constructor is passed a String of less than 2 characters in length, it will throw an exception

A better version might be:

Greet( String who ) { 
  name = who.inject( '' ) { String s, String c ->
    s += s ? c.toLowerCase() : c.toUpperCase()
  }
}

As metioned in the comments,

Greet( String who ) { 
  name = who.capitalize()
}

is the proper way


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...