2011-05-24 40 views
2

刪除屬性假設我有MyClass的領域類:的Grails如何從對象

class MyClass { 
    String prop1 
    String prop2 
    String prop3 
} 

我不知道有沒有什麼辦法刪除從MyClass的對象例如爲prop1屬性?

回答

4

實際刪除該屬性的唯一方法是將其從源文件中刪除。但是,您可以嘗試訪問該屬性表現出與嘗試訪問不存在的屬性相同的行爲。

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")} 
}