Groovyhtml
Outline
• Hello world
• Smooth Java integration
• Flat learning curve
• Powerful featuresspring
跟 Java ⽆無縫集成
• 呃, 實際上仍是有⼀一點差異的, 看這⾥裏http://www.groovy-lang.org/differences.html
• 實際項⺫⽬目展⽰示數組
平滑的學習曲線
• Style guide http://www.groovy-lang.org/style-guide.html
• Design patterns http://www.groovy-lang.org/design-patterns.html閉包
反射
import org.springframework.util.ReflectionUtils;
Method[] methods = ReflectionUtils.getAllDeclaredMethods(interfaceClazz);
Method method = null;
for (Method m : methods) {
if (methodName.equals(m.getName())) {
method = m;
break;
}
}
if (method != null) {
if (params != null && !"null".equals(params)) {
Type[] paramTypes = method.getGenericParameterTypes();
val = ReflectionUtils.invokeMethod(method, service, deserialize(params,
paramTypes).toArray());
} else {
val = ReflectionUtils.invokeMethod(method, service);
}
} else{
throw new MethodNotFoundException();
}
—————————————————————————————————————————————————————————————————————————————————————————————————————
Type[] paramTypes = interfaceClazz.methods.find{
it.name == methodName
}.getGenericParameterTypes()
val = service."$methodName"(* deserialize(params,paramTypes))ide
閉包
• 調薪函數
其餘
• 構造函數
• ?.操做符(空指針異常)
• *.操做符
• 魔幻數組(+,-,去重,鏈接)
• with ⽅方法
• @Category與@Mixin
• 綜合(獲取購物⻋車中全部總價⼤大於7000的商品名稱)學習
class Cart{
• List cartItems
• }
• class CartItem{
• Product product
• int amount
• }
• class Product{
• String name
• int price
• }
• new Cart(
• cartItems:[
• new CartItem(product:new Product(name:"aa",price:4000),amount:2),
• new CartItem(product:new Product(name:"bb",price:3000),amount:20)
• ]
• ).cartItems.grep{
• it.product.price * it.amount > 7000
• }.product?.nameui
動靜皆宜
• @CompileStatic
• @Grab指針