标签:详解 aos depend dna getname pen col 开发人员 class
在Java中,封装是一个非常好的机制,最常见的封装莫过于get,set方法了,无论是Intellij idea 还是Eclipse,都提供了快速生成get,set方法的快捷键,使用起来很是方便,其实,我们还有更方便的办法,那就是-Lombok:非常强大的POJO注解器。
lombok 提供了简单的注解的形式来帮助我们简化消除一些必须有但显得很臃肿的 java 代码。特别是相对于 POJO。
使用 lombok 是需要安装的,如果不安装,IDE 则无法解析 lombok 注解。先在官网下载最新版本的 JAR 包。
-Xbootclasspath/a:lombok.jar
-javaagent:lombok.jar
<dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> <version>1.18.0</version> </dependency>
注解在类上;提供类所有属性的 getting 和 setting 方法,此外还提供了equals、canEqual、hashCode、toString 方法,相当于同时加上以下注解@Setter @Getter,@ToString,@EqualsAndHashCode
1 @Data 2 public class Person { 3 private String name; 4 private String address; 5 private String city; 6 private String state; 7 private String zip; 8 private Date brithday; 9 }
效果如下:
注解在类和属性上;为属性提供 setting、getting 方法
public class Person { @Getter@Setter private String name; }
等价源码:
1 public String getName() { 2 return name; 3 } 4 5 public void setName(String name) { 6 this.name = name; 7 }
生成toString方法,默认情况下,会输出类名、所有属性,属性按照顺序输出,以逗号分割。但需要注意的是:@ToString有多个属性可以进一步设置:
callSuper 是否输出父类的toString方法,默认为false
includeFieldNames 是否包含字段名称,默认为true
exclude 排除生成tostring的字段
使用方法:
1 @ToString(callSuper = true,exclude ={"name"}) 2 public class Person { 3 private String name; 4 private String address; 5 }
等价源码:
1 public String toString() { 2 return "Person{" + 3 "address=‘" + address + ‘\‘‘ + 4 ‘}‘; 5 }
该注解快速判断是否为空,如果为空,则抛出java.lang.NullPointerException
使用方法
1 public class Person { 2 3 private String name; 4 5 @Setter@Getter@NonNull 6 private List<Person> member; 7 }
等价源码:
1 @NonNull 2 private List<Person> members; 3 4 public Family(@NonNull final List<Person> members) { 5 if (members == null) throw new java.lang.NullPointerException("members"); 6 this.members = members; 7 } 8 9 @NonNull 10 public List<Person> getMembers() { 11 return members; 12 } 13 14 public void setMembers(@NonNull final List<Person> members) { 15 if (members == null) throw new java.lang.NullPointerException("members"); 16 this.members = members; 17 }
该注解自动添加到同步机制,有趣的是,生成的代码并不是直接锁方法,而是锁代码块, 作用范围是方法上。
使用方法:
1 private DateFormat format = new SimpleDateFormat("MM-dd-YYYY"); 2 3 @Synchronized 4 public String synchronizedFormat(Date date) { 5 return format.format(date); 6 }
等价源码:
1 private final java.lang.Object $lock = new java.lang.Object[0]; 2 private DateFormat format = new SimpleDateFormat("MM-dd-YYYY"); 3 4 public String synchronizedFormat(Date date) { 5 synchronized ($lock) { 6 return format.format(date); 7 } 8 }
注释可用于确保已分配的资源被释放,如IO的连接关闭。
使用方法:
1 public void testCleanUp() { 2 try { 3 @Cleanup ByteArrayOutputStream baos = new ByteArrayOutputStream(); 4 baos.write(new byte[] {‘Y‘,‘e‘,‘s‘}); 5 System.out.println(baos.toString()); 6 } catch (IOException e) { 7 e.printStackTrace(); 8 } 9 }
等价源码:
1 public void testCleanUp() { 2 try { 3 ByteArrayOutputStream baos = new ByteArrayOutputStream(); 4 try { 5 baos.write(new byte[]{‘Y‘, ‘e‘, ‘s‘}); 6 System.out.println(baos.toString()); 7 } finally { 8 baos.close(); 9 } 10 } catch (IOException e) { 11 e.printStackTrace(); 12 } 13 }
使用链式结构
1 public class Student { 2 private String name; 3 private int age; 4 5 public String getName() { 6 return name; 7 } 8 9 public Student setName(String name) { 10 this.name = name; 11 return this; 12 } 13 14 public int getAge() { 15 return age; 16 } 17 18 public Student setAge(int age) { 19 return this; 20 } 21 }
1 @Accessors(chain = true) 2 @Data 3 @NoArgsConstructor(staticName = "of") 4 public class Student { 5 private String name; 6 private int age; 7 }
1 Student student = Student.of().setName("wsk").setAge(12);
使用builder,构建 建造者模式
1 @Builder 2 public class Student { 3 private String name; 4 private int age; 5 }
1 Student student = Student.builder().name("zs").age(24).build();
1 public class Student { 2 private String name; 3 private int age; 4 5 public String getName() { 6 return name; 7 } 8 9 public void setName(String name) { 10 this.name = name; 11 } 12 13 public int getAge() { 14 return age; 15 } 16 17 public void setAge(int age) { 18 this.age = age; 19 } 20 21 public static Builder builder(){ 22 return new Builder(); 23 } 24 public static class Builder{ 25 private String name; 26 private int age; 27 public Builder name(String name){ 28 this.name = name; 29 return this; 30 } 31 32 public Builder age(int age){ 33 this.age = age; 34 return this; 35 } 36 37 public Student build(){ 38 Student student = new Student(); 39 student.setAge(age); 40 student.setName(name); 41 return student; 42 } 43 } 44 }
1 public class Student { 2 private String id; 3 private String name; 4 private String sex; 5 private int age; 6 private String department; 7 8 public static class Builder { 9 /* 10 * 只能指定一次。 11 */ 12 private final String id; 13 private final String department; 14 15 private String name = ""; 16 private String sex = "男"; 17 private int age = 20; 18 19 /* 20 * 非空属性,必须在构造器中指定。 21 */ 22 public Builder(String id, String department) { 23 this.id = id; 24 this.department = department; 25 } 26 27 /* 28 * name,sex,age可选择属性,提供特殊的setter方法。 29 */ 30 public Builder name(String name) { 31 this.name = name; 32 return this; 33 } 34 35 public Builder sex(String sex) { 36 this.sex = sex; 37 return this; 38 } 39 40 public Builder age(int age) { 41 this.age = age; 42 return this; 43 } 44 45 /* 46 * Student对象创建器,想得到一个Student对象必须使用build 方法, 47 * 在方法中增加对Builder参数的验证,并以异常的形式告诉给开发人员。 48 */ 49 public Student build() { 50 /* 检查Builder对象中的数据是否合法。 51 * 针对这个例子,就是检查主键冲突,外键制约等 52 * 如果不满足我们可以抛出一个IllegalArgumentException 53 */ 54 return new Student(this); 55 56 } 57 58 } 59 60 private Student(Builder builder) { 61 this.id = builder.id; 62 this.name = builder.name; 63 this.sex = builder.sex; 64 this.age = builder.age; 65 this.department = builder.department; 66 } 67 68 /* 69 * 只提供getter方法 70 */ 71 public String getId() { 72 return id; 73 } 74 75 public String getName() { 76 return name; 77 } 78 79 public String getSex() { 80 return sex; 81 } 82 83 public int getAge() { 84 return age; 85 } 86 87 public String getDepartment() { 88 return department; 89 } 90 91 }
1 student = new Student.Builder("03041013", "计算机").name("李华").build();
1 Student.Builder builder = new Student.Builder("03041013", "计算机"); 2 builder.name("李华"); 3 Student student = builder.build();
标签:详解 aos depend dna getname pen col 开发人员 class
原文地址:https://www.cnblogs.com/cb0327/p/10040671.html