有效的方式来访问/存储数据,而不是有一个巨大的构造函数类

我目前需要一些指导。 而不是使用gets / sets来创建一个巨大的构造器类。 有没有可能简化这个任务?

试图避免有一个巨大的构造函数获取/设置。 所以我假设什么是避免这样做的好方法。 这种事情怎么能被大大简化呢?

public User(int id, String name, long skillPoints) { this.id = id; this.name = name; this.skillPoints = skillPoints; this.level = 0; // So on so forth } 

你有没有听说过龙目岛项目 ? 通过添加注释@Data,您将获得所有字段的@ToString,@EqualsAndHashCode,@Getter,所有非final字段的@Setter和@RequiredArgsConstructor的快捷方式。 还有更多的注释,你可以检查出来!

与龙目岛

 import lombok.AccessLevel; import lombok.Setter; import lombok.Data; import lombok.ToString; @Data public class DataExample { private final String name; @Setter(AccessLevel.PACKAGE) private int age; private double score; private String[] tags; @ToString(includeFieldNames=true) @Data(staticConstructor="of") public static class Exercise<T> { private final String name; private final T value; } } 

香草爪哇

 import java.util.Arrays; 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; } 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; } @Override public String toString() { return "DataExample(" + this.getName() + ", " + this.getAge() + ", " + this.getScore() + ", " + Arrays.deepToString(this.getTags()) + ")"; } protected boolean canEqual(Object other) { return other instanceof DataExample; } @Override public boolean equals(Object o) { if (o == this) return true; if (!(o instanceof DataExample)) return false; DataExample other = (DataExample) o; if (!other.canEqual((Object)this)) return false; if (this.getName() == null ? other.getName() != null : !this.getName().equals(other.getName())) return false; if (this.getAge() != other.getAge()) return false; if (Double.compare(this.getScore(), other.getScore()) != 0) return false; if (!Arrays.deepEquals(this.getTags(), other.getTags())) return false; return true; } @Override public int hashCode() { final int PRIME = 59; int result = 1; final long temp1 = Double.doubleToLongBits(this.getScore()); result = (result*PRIME) + (this.getName() == null ? 43 : this.getName().hashCode()); result = (result*PRIME) + this.getAge(); result = (result*PRIME) + (int)(temp1 ^ (temp1 >>> 32)); result = (result*PRIME) + Arrays.deepHashCode(this.getTags()); return result; } public static class Exercise<T> { private final String name; private final T value; private Exercise(String name, T value) { this.name = name; this.value = value; } public static <T> Exercise<T> of(String name, T value) { return new Exercise<T>(name, value); } public String getName() { return this.name; } public T getValue() { return this.value; } @Override public String toString() { return "Exercise(name=" + this.getName() + ", value=" + this.getValue() + ")"; } protected boolean canEqual(Object other) { return other instanceof Exercise; } @Override public boolean equals(Object o) { if (o == this) return true; if (!(o instanceof Exercise)) return false; Exercise<?> other = (Exercise<?>) o; if (!other.canEqual((Object)this)) return false; if (this.getName() == null ? other.getValue() != null : !this.getName().equals(other.getName())) return false; if (this.getValue() == null ? other.getValue() != null : !this.getValue().equals(other.getValue())) return false; return true; } @Override public int hashCode() { final int PRIME = 59; int result = 1; result = (result*PRIME) + (this.getName() == null ? 43 : this.getName().hashCode()); result = (result*PRIME) + (this.getValue() == null ? 43 : this.getValue().hashCode()); return result; } } } 

将Kotlin添加到您的项目中,正在成为标准,将您的问题解决为一种魅力,正如Google正式支持的那样,如果您正在进行生产,而不是使用其他库(可能有错误),则没有任何问题。 不要以为你不能把所有的项目从Java转换到Kotlin,因为Kotlin是100%兼容的。 Kotlin的K特性之一就是解决你的问题:避免将构造函数链接到实例变量和getter和setter,是很多的锅炉代码。 你只需要将Kotlin添加到你的项目中,花费不到3分钟,那么你只能改变POJO类,这是你指向构造函数,getter和setter的普通类的名字/首字母缩写词。

在安装Kotlin之后,以这种方式使用数据类 ,如下所示的86行的类将成为一行。 即使您不打算将Kotlin实施到您的项目的其余部分值得这样做

 public class Movie { private String name; private String studio; private float rating; public Movie(String name, String studio, float rating) { this.name = name; this.studio = studio; this.rating = rating; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getStudio() { return studio; } public void setStudio(String studio) { this.studio = studio; } public float getRating() { return rating; } public void setRating(float rating) { this.rating = rating; } @Override public int hashCode() { final int prime = 31; int result = 1; result = prime * result + ((name == null) ? 0 : name.hashCode()); result = prime * result + Float.floatToIntBits(rating); result = prime * result + ((studio == null) ? 0 : studio.hashCode()); return result; } @Override public boolean equals(Object obj) { if (this == obj) return true; if (obj == null) return false; if (getClass() != obj.getClass()) return false; Movie other = (Movie) obj; if (name == null) { if (other.name != null) return false; } else if (!name.equals(other.name)) return false; if (Float.floatToIntBits(rating) != Float.floatToIntBits(other.rating)) return false; if (studio == null) { if (other.studio != null) return false; } else if (!studio.equals(other.studio)) return false; return true; } @Override public String toString() { return "Movie [name=" + name + ", studio=" + studio + ", rating=" + rating + "]"; } } 

将成为只是这个 ,并将获得免费也toHashtoString

 data class Movie(var name: String, var studio: String, var rating: Float)