LomBok表达式一:val

OverView

You can use val as the type of a local variable declaration instead of actually writing the type. When you do this, the type will be inferred from the initializer expression. The local variable will also be made final. This feature works on local variables and on foreach loops only, not on fields. The initializer expression is required.

val is actually a 'type' of sorts, and exists as a real class in the lombok package. You must import it for val to work (or uselombok.val as the type). The existence of this type on a local variable declaration triggers both the adding of the final keyword as well as copying the type of the initializing expression which overwrites the 'fake' val type.

WARNING: This feature does not currently work in NetBeans.


概述

你可以使用 val 声明本地变量的类型而不用写出它的实际类型,它实际的类型将由初始化的表达式推断。局部变量将被声明为 final 。这个功能只在局部变量和foreach循环中奏效,在字段中不奏效。并且初始化表达式是必须的。

val 实际上是实际的类型,它是一个真实的类并可以在 Lombok 的包中找到,你使用 val 前必须先导包。

警告:这个注解在 NetBean 中并不能正确的运行


综上所述:1:val Quote = new AnyObject(); [必须声明后面的表达式]

                    2:只能声明局部变量

                    3:声明出来的局部变量为 final 类型



由于这个功能不能再 IDEA 中正确运行,小伙伴们可以尝试一下在 Eclipse系列的产品中尝试运行一下



官网 Demo

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • **2014真题Directions:Read the following text. Choose the be...
    又是夜半惊坐起阅读 10,066评论 0 23
  • 情不知所起,一往情深,生者可以死,死者可以生。
    路飞hjkl阅读 231评论 0 0
  • 老張在星期一的湖底仰臥 老張 星期天 一個人工湖 老張會寫詩 老張的手,老張的手臂,彎曲,線條狀,交錯綿綿,老張的...
    涵八阅读 245评论 2 2
  • 与其浑浑噩噩的过日子,不如发现生活中的点滴美好,做自己想做的事,保持对未知事物的探索心理,保持对生活的热情。
    wuli小葛阅读 259评论 0 2