lombok的@Builder创建或修改对象新玩法
pom依赖 <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> <version>0.10.2</version> </dependency>
创建或修改对象新玩法
import lombok.*; import lombok.extern.slf4j.Slf4j; import com.alibaba.fastjson.JSON; @Getter @Slf4j @ToString @Builder(toBuilder = true) public class TestBuilderController { private String name; private int age; public static void main(String[] args) { TestBuilderController testBuilderController = TestBuilderController.builder().age(23).name("small").build(); log.info("创建【{}】",JSON.toJSONString(testBuilderController)); testBuilderController= testBuilderController.toBuilder().age(24).name("sun").build(); log.info("修改【{}】",JSON.toJSONString(testBuilderController)); } }
执行结果:
创建【{"age":23,"name":"small"}】 修改【{"age":24,"name":"sun"}】
缘于生活,而归于工作。本人所书,而意于分享。
如有转载,请注明出处!
--活出自己范儿