In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-27 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
This article mainly introduces the relevant knowledge of "how to simplify java code". The editor shows you the operation process through an actual case. The operation method is simple, fast and practical. I hope this article "how to simplify java code" can help you solve the problem.
Principle
Lombok can be annotated to generate constructor, getter/setter, equals, hashcode, toString methods for attributes at compile time. So we don't have to write these methods manually.
Quote
To use lombok, you need to refer to the jar package, which can be downloaded from the official website, or you can use maven to add corresponding dependencies.
Maven dependencies:
Org.projectlombok lombok 1.16.20 provided use
Using the @ Data annotation on a class automatically generates setter/getter, equals, canEqual, hashCode, toString methods for all properties of the class (if it is a final property, no setter method is generated for that property).
Public class DataExample {private final String name; private int age; private double score; private String [] tags; public DataExample (String name) {this.name = name;} public String getName () {return this.name;} public void setAge (int age) {this.age = age;} public int getAge () {return this.age;} public void setScore (double score) {this.score = score;} public double getScore () {return this.score } public String [] getTags () {return this.tags;} public void setTags (String [] tags) {this.tags = tags;}}
The above code is a simple pojo class, when we use lombok:
Import lombok.AllArgsConstructor;import lombok.Data;import lombok.EqualsAndHashCode;import lombok.NoArgsConstructor;import lombok.ToString;@Datapublic class DataExample {private final String name; private int age; private double score; private String [] tags;}
Did you find anything? The amount of code has been halved in an instant? In fact, the code generated with @ Data is much more than that. He has also generated methods of equals, canEqual, hashCode, and toString, which can be found by trying to call them in IDE.
The goal of the @ Data annotation is to generate all the code that needs to be generated automatically, but sometimes we don't need to generate it completely, so we can use a single modifier to annotate it.
@ Data brings together all the features of @ ToString, @ EqualsAndHashCode, @ Getter/@Setter, and @ RequiredArgsConstructor. In other words, these annotations can also be used individually, so let's take @ Getter/@Setter as an example to show how the above code can be simplified.
That's all for import lombok.AllArgsConstructor;import lombok.Data;import lombok.EqualsAndHashCode;import lombok.NoArgsConstructor;import lombok.ToString;public class DataExample {@ Getter private final String name; @ Getter @ Setter private int age; @ Getter @ Setter private double score; @ Getter @ Setter private String [] tags;} on "how to simplify java code". Thank you for reading. If you want to know more about the industry, you can follow the industry information channel. The editor will update different knowledge points for you every day.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.