我想调用一个名称在静态上下文中包含空格的方法,但它不起作用。 有什么建议吗?
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)
答案 0 :(得分:3)
它将以这种方式工作:
class Test2 {
void "test a"() {
println "test a"
}
public static void main(String[] args) {
def t = new Test2()
def v = 'test a'
t."$v"()
}
}
答案 1 :(得分:2)
invokeMethod
怎么样?
class Invokes {
def "test a"() {
"test a"
}
static main(args) {
def t = new Invokes()
assert t.invokeMethod("test a", null) == "test a"
}
}