【发布时间】:2020-05-05 15:02:46
【问题描述】:
我在 Groovy 中阅读了 use 关键字。但无法拿出来,因为它已经完全被使用了。而且我还附带了 category 类,在这个主题下,那也是什么?来自 Groovy In Action
class StringCalculationCategory {
static def plus(String self, String operand) {
try {
return self.toInteger() + operand.toInteger()
} catch (NumberFormatException fallback) {
return (self << operand).toString()
}
}
}
use (StringCalculationCategory) {
assert 1 == '1' + '0'
assert 2 == '1' + '1'
assert 'x1' == 'x' + '1'
}
有了上面的代码,谁能说说groovy中use关键字有什么用?还有上面的代码是做什么的?
【问题讨论】:
-
@simon:你可以找到一本很棒的书!
-
@niccolom。我已经将它与 Java 一起使用了大约 5 年,我会说它对于某些类型的代码是值得的。 Java 适合团队开发,Groovy 更适合快速编写脚本和灵活解决问题的方法。大多数情况下,它只是一种清洗,但偶尔使用 StringBuilder 或 File.text 之类的帮助程序使其非常值得用于脚本、原型和测试。
标签: groovy