访问/存储数据的有效方式,而不是拥有庞大的构造函数类

时间:2017-11-11 15:12:35

标签: java android sqlite

我目前需要一些指导。而不是使用gets / sets创建一个巨大的构造函数类。是否可以简化此任务?

尽量避免使用带有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
}

2 个答案:

答案 0 :(得分:0)

您听说过Project Lombok吗? 通过添加注释@Data,您将获得所有字段的@ToString,@ EqualsAndHashCode,@ Getter,所有非最终字段的@Setter和@RequiredArgsConstructor的快捷方式。还有更多可以查看的注释!

使用Lombok

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;
  }
}

Vanilla Java

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;
    }
  }
}

答案 1 :(得分:-1)

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

安装Kotlin后,使用Data Classes 通过这种方式,如下所示的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 + "]";
}
}

会变得公正 this并且还将免费获得toHashtoString

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