Grails(19)instanceOf in Grails
This function will only work in integration test case. This method is based on the GORM.
@Test
publicvoid testInstanceOf(){
DatabaseObject databaseObject = new DatabaseObject()
assertTrue !(databaseObjectinstanceof DatabaseObject2 )
assertTrue "asdfasdf"instanceof String
TenantUtils.doWithTenant(brand.idas Integer) {
assertTrue !(event.instanceOf(DatabaseObject2.class))
}
assertTrue true
}
What is the advantage of instanceOf than instanceof.
class Container {
static hasMany = [children: Child]
}
class Child {
String name
static belongsTo = [container: Container]
}
class Thing extends Child {}
class Other extends Child {}
def container = Container.get(id)
for (child in container.children) {
if(child.instanceOf(Thing)){
// thing
}
else if(child.instanceOf(Other)){
// other
}
}
It is good to learn that in groovy world.
References:
http://www.grails.org/doc/1.3.7/ref/Domain%20Classes/instanceOf.html
This function will only work in integration test case. This method is based on the GORM.
@Test
publicvoid testInstanceOf(){
DatabaseObject databaseObject = new DatabaseObject()
assertTrue !(databaseObjectinstanceof DatabaseObject2 )
assertTrue "asdfasdf"instanceof String
TenantUtils.doWithTenant(brand.idas Integer) {
assertTrue !(event.instanceOf(DatabaseObject2.class))
}
assertTrue true
}
What is the advantage of instanceOf than instanceof.
class Container {
static hasMany = [children: Child]
}
class Child {
String name
static belongsTo = [container: Container]
}
class Thing extends Child {}
class Other extends Child {}
def container = Container.get(id)
for (child in container.children) {
if(child.instanceOf(Thing)){
// thing
}
else if(child.instanceOf(Other)){
// other
}
}
It is good to learn that in groovy world.
References:
http://www.grails.org/doc/1.3.7/ref/Domain%20Classes/instanceOf.html
本文深入探讨了Groovy编程语言中instanceOf方法的使用与优势,通过实例解析其在不同场景下的应用,包括如何在TenantUtils类中进行基于ID的实例判断,以及如何在循环遍历容器对象时进行类型区分。了解instanceOf在Groovy世界中的应用,对于提升代码的灵活性和效率至关重要。
1117

被折叠的 条评论
为什么被折叠?



