gpt4 book ai didi

java - 默认情况下,Groovy 自动装箱 boolean 值是否为对象?

转载 作者:行者123 更新时间:2023-11-30 06:52:12 25 4
gpt4 key购买 nike

考虑这个 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");
}

}

常规类:

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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com