加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 大数据 > 正文

groovy – 不可变和最终之间的区别

发布时间:2020-12-14 16:29:57 所属栏目:大数据 来源:网络整理
导读:不可变和最终有什么区别? 例如,这个 @Immutablepublic MyClass { String property1 MyOtherClass property2 List myLIst} 和 public final MyClass { final String property1 final MyOtherClass property2 final List myLIst} 解决方法 The @Immutable ann
不可变和最终有什么区别?

例如,这个

@Immutable
public MyClass {
   String property1 
   MyOtherClass property2
   List myLIst
}

public final MyClass {
   final String property1 
   final MyOtherClass property2
   final List myLIst
}

解决方法

The @Immutable annotation instructs the compiler to execute an AST transformation which adds the necessary getters,constructors,equals,hashCode and other helper methods that are typically written when creating immutable classes with the defined properties.
[1]

因此,@ Immrange会生成辅助功能,类似于Scala中的“case classes”.
final关键字指示编译器特定变量是不可变的,就像在Java中一样.

第一个类等同于具有多个辅助函数的第二个类.

[1] http://groovy.codehaus.org/gapi/groovy/transform/Immutable.html

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读