开源之夏第三届火热来袭,高校学生参与赢万元奖金!>>>
static void main(String[] args) { def one2 = "1 + 2 == ${m -> m << 3}" println "one2 是否匹配" assert one2 == "1 + 2 == 3" println "one2 匹配了" def one1 = "1 + 2 == ${m -> m}" << 3 println "one1 是否匹配" assert one1 == "1 + 2 == 3" }
请问,为什么会出现不同的结果呀?
回答
因为这俩类型都不一样,在groovyConsole可以很轻松验证这一点:
用assert one1 as String == "1 + 2 == 3"
或者 assert one1.toString() == "1 + 2 == 3" 就没问题了
而one2是GString类型
GString类型在GDK中进行了扩展:https://groovy-lang.org/syntax.html#_interoperability_with_java
是可以和String类型直接比对的(但是hashCode不同)
发表评论