(快速參考)

instanceOf

目的

判斷網域類別實例是否為指定類別的實例,如果實例是代理程式,則解析實際類別。

範例

假設有網域類別

class Container {
   static hasMany = [children: Child]
}
class Child {
   String name
   static belongsTo = [container: Container]
}
class Thing extends Child {}
class Other extends Child {}

然後你可以使用下列方式判斷 `Container` 的 `children` 集合中元素的類型

def container = Container.get(id)
for (child in container.children) {
   if (child.instanceOf(Thing)) {
      // process Thing
   }
   else if (child.instanceOf(Other)) {
      // process Other
   }
   else {
      // handle unexpected type
   }
}

說明

參數

  • clazz - 要檢查的類型