考虑这个 Java 类:
public class Demo {
public void a(boolean a){
System.out.println("boolean was called");
}
public void a(Object a){
System.out.println("Object was called");
}
}
Groovy 类:
class Groovy {
static void main(String[] args) {
def demo = new Demo()
demo.a(true)
}
}
输出:
Object was called
在 Groovy 中为其编写测试并传入原语 true
来调用 a(Object)
。
这是预期的行为吗?我如何调用其他方法? (我使用的是2.4.6版本)
仅供引用,这会导致 EasyMock 出现问题 (# 175890293 )
最佳答案
我想说这是有意的,请参阅 groovy 手册:
http://groovy-lang.org/objectorientation.html
Groovy supports the same primitive types as those defined by the Java Language Specification:
[...]
boolean type (exactly true or false)
[...]
While Groovy declares and stores primitive fields and variables as primitives, because it uses Objects for everything, it autowraps references to primitives. Just like Java, the wrappers it uses are
Table 1. primitive wrappers
Primitive type Wrapper class
boolean Boolean
关于java - 默认情况下,Groovy 是否自动将 boolean 值装箱为对象?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39407516/