Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/344.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 用于比较两个对象的类的扩展_Java - Fatal编程技术网

Java 用于比较两个对象的类的扩展

Java 用于比较两个对象的类的扩展,java,Java,假设我在Java上有以下类: public class Entity<T extends Comparable<T>> { private T t; private int id; private Entity<T> innerEntity; public Entity(T t) { this(t, 100, null); } public Entity(T t, int id) { this(t,id,

假设我在Java上有以下类:

public class Entity<T extends Comparable<T>> { 
  private T t; 
  private int id; 
  private Entity<T> innerEntity; 

  public Entity(T t) {
    this(t, 100, null); 
  } 

  public Entity(T t, int id) {
    this(t,id, null); 
  }

  public Entity(T t, int id, Entity<T> innerEntity) {
    this.t = t; 
    this.id = id; 
    this.innerEntity = innerEntity; 
  } 

  public T getT() {
    return t; 
  }

  public void setT(T t) {
    this.t = t; 
  }

  public int getId() {
    return id; 
  }

  public void setId(int id) {
    this.id = id;
  } 

  public Entity<T> getInnerEntity() {
    return innerEntity;
  } 

  public void setInnerEntity(Entity<T> innerEntity) {
    this.innerEntity = innerEntity; 
  } 

  @Override public boolean equals(Object o) {
    if (this == o) 
      return true; 
    if (!(o instanceof Entity)) 
      return false; 

    Entity<?> entity = (Entity<?>) o; 

    if (id != entity.id) 
      return false; 
    if (!t.equals(entity.t)) 
      return false; 
    return innerEntity.equals(entity.innerEntity); 

  }

  @Override public int hashCode() {
    int result = t.hashCode(); 

    result = 31 * result + id; 
    result = 31 * result + innerEntity.hashCode(); 
    return result; 
  }  
}
公共类实体{
私人T;
私有int-id;
私人实体;
公共实体(T){
这(t,100,null);
} 
公共实体(T,int id){
这(t,id,null);
}
公共实体(T,int-id,Entity-innerEntity){
t=t;
this.id=id;
this.innerEntity=innerEntity;
} 
公共T getT(){
返回t;
}
公共无效集合(T){
t=t;
}
公共int getId(){
返回id;
}
公共无效集合id(内部id){
this.id=id;
} 
公共实体getInnerEntity(){
返回内部实体;
} 
公共void setInnerEntity(Entity innerEntity){
this.innerEntity=innerEntity;
} 
@重写公共布尔等于(对象o){
if(this==o)
返回true;
如果(!(实体的实例))
返回false;
实体=(实体)o;
if(id!=entity.id)
返回false;
如果(!t.equals(entity.t))
返回false;
返回innerEntity.equals(entity.innerEntity);
}
@重写公共int hashCode(){
int result=t.hashCode();
结果=31*result+id;
result=31*result+innerEntity.hashCode();
返回结果;
}  
}
我们如何扩展该类,使其类型的对象列表可以移动到Collections.sort()方法,,而无需比较器,并且可以按以下顺序比较任意两个对象:

  • 通过T参数值
  • 按id(int类型)
  • 按实体

  • 从键入的角度来看,您需要添加

     implements Comparable<Entity<T>> 
    
    实现可比较的
    
    然后实现接口所需的compareTo(实体)方法

    你在评论中质疑这句话:

    我是否应该使用实体类实现
    可比性
    ?如果我使用EntityComp实现的
    Compariable
    不正确,则类型应相等

    我想您是说您应该能够比较具有不同类型参数的
    实体
    实例

    首先,(对我来说)这门课实际上代表什么并不清楚。从代码本身看,这并不明显。(也许javadoc注释会有所帮助?)

    您的想法的问题是
    实现可比较的
    会给您编译警告。。。关于原始类型


    实现可比性“可通过T参数值进行比较”是什么意思?您需要添加
    实现可比性
    ,然后实现
    比较(实体)
    接口所需的方法。@StephenC但是我应该实现Compariable with entity类吗?如果我实现Compariable with EntityComp,则类型应该相等