【Groovy】入门布道ppt

Groovy

Outline
• Hello world
• Smooth Java integration
• Flat learning curve
• Powerful features

跟 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))

闭包
• 调薪

其他
• 构造函数
• ?.操作符(空指针异常)
• *.操作符
• 魔幻数组(+,-,去重,连接)
• 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?.name

动静皆宜
• @CompileStatic
• @Grab

上一篇:FFT学习笔记


下一篇:spring aop的配置