grails如何从对象中删除属性

Bel*_*lla 2 grails groovy properties

假设我有MyClass域类:

class MyClass {
  String prop1
  String prop2
  String prop3
}
Run Code Online (Sandbox Code Playgroud)

我想知道有什么方法可以从MyClass对象中删除例如prop1属性吗?

Dón*_*nal 5

实际删除该属性的唯一方法是将其从源文件中删除。但是,您可以使尝试访问该属性表现出与尝试访问不存在的属性相同的行为。

class MyClass {

  String prop1
  String prop2
  String prop3
}

MyClass.metaClass {
  // Intercept attempts to get the property
  getProp1 = {-> throw new MissingPropertyException("can't get prop1")}
  // Intercept attempts to set the property
  setProp1 = {throw new MissingPropertyException("can't set prop1")}
}
Run Code Online (Sandbox Code Playgroud)