Groovy与Grails框架技术详解
1. Groovy运算符及相关特性
Groovy在Java标准运算符集的基础上增加了多个运算符,这些运算符为开发者提供了更便捷、灵活的编程方式。
1.1 空安全解引用运算符(?.)
这是最常用的运算符之一,它能让你在调用空对象的方法或访问其属性时避免 NullPointerException 。在链式访问中,若链中某个值可能为 null ,使用该运算符就非常有用。
String name = person?.organization?.parent?.name
若 person 、 person.organization 或 organization.parent 为 null ,则表达式返回 null 。而Java的实现方式则较为冗长:
String name = null;
if (person != null) {
if (person.getOrganization() != null) {
if (person.getOrganization().getParent() != null) {
name = person.getOrganization().getParent().
超级会员免费看
订阅专栏 解锁全文
64

被折叠的 条评论
为什么被折叠?



