gpt4 book ai didi

groovy 调用方法名称包含特殊字符

转载 作者:行者123 更新时间:2023-12-01 11:36:22 24 4
gpt4 key购买 nike

我想在静态上下文中调用名称包含空格的方法,但它不起作用。有什么建议吗?

class Test2 { 
void "test"() {
"test a"()
}

void "test a"() {
println "test a"
}

public static void main(String[] args) {
def t = new Test2()
t."test"() //it works
t."test a"() //raise error, Illegal class name "Test2$test a" in class file Test2$test a
}
}

G:\tmp\groovy\gp1\src>groovy -version
Groovy Version: 2.3.2 JVM: 1.7.0_02 Vendor: Oracle Corporation OS: Windows 7

G:\tmp\groovy\gp1\src>groovy Test2.groovy
Test1.main
Caught: java.lang.ClassFormatError: Illegal class name "Test2$test a" in class file Test2$test a
java.lang.ClassFormatError: Illegal class name "Test2$test a" in class file Test2$test a
at Test2.main(Test2.groovy:15)

最佳答案

它将以这种方式工作:

class Test2 { 

void "test a"() {
println "test a"
}

public static void main(String[] args) {
def t = new Test2()
def v = 'test a'
t."$v"()
}
}

如所述herehere ,但不知道为什么您的示例不起作用。

关于groovy 调用方法名称包含特殊字符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26751703/

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