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

后端 未结 1 639
既然无缘
既然无缘 2021-02-20 03:34

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:24

    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)
提交回复
热议问题