Can Groovy dynamically add or override a method on a POJO?

后端 未结 1 700
北恋
北恋 2021-02-20 03:52

If I have

// java
class MyClass {
   public String getName() {
     return \"hector\";
   }
}

and an instance of this class. Can Groovy overri

相关标签:
1条回答
  • 2021-02-20 04:38

    Of course you can using Dynamic MetaClass.

    Your case is specifically covered by the following example :

    def object = new MyClass();
    object.metaClass.getName = { "Jake" }
    assert "Jake" == object.getName()
    
    0 讨论(0)
提交回复
热议问题