Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/gwt/3.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
TIJ4-compareTo()和继承-Tuple.java,第17章练习28_Java - Fatal编程技术网

TIJ4-compareTo()和继承-Tuple.java,第17章练习28

TIJ4-compareTo()和继承-Tuple.java,第17章练习28,java,Java,第17章练习28的TIJ4解决方案指南说: 当一个类扩展了一个具体的可比类(如T3 扩展了原始程序中的T2),并添加了 字段,无法同时实现compareTo() 正确地(…)同样适用于equals() 我使用继承做了这个练习,因为它在“原始”中使用 在练习中,有些需要实施 equals()方法和可比较接口 public class TwoTuple<A extends Comparable, B extends Comparable> implements Comparab

第17章练习28的TIJ4解决方案指南说:

当一个类扩展了一个具体的可比类(如T3 扩展了原始程序中的T2),并添加了 字段,无法同时实现compareTo() 正确地(…)同样适用于equals()

我使用继承做了这个练习,因为它在“原始”中使用 在练习中,有些需要实施 equals()方法和可比较接口

public class TwoTuple<A extends Comparable, B extends Comparable> 
    implements Comparable {
  public final A first;
  public final B second;
  public TwoTuple(A a, B b) { 
    first = a; 
    second = b;
  }
  public String toString() {
    return "(" + toString2() + ")";
  }
  protected String toString2() { 
    return first + ", " + second;
  }
  public boolean equals(Object o) {
    return o != null && 
      o.getClass() == getClass() && 
      (first == null 
        ? getClass().cast(o).first == null 
        : first.equals(getClass().cast(o).first)) &&
      (second == null 
        ? getClass().cast(o).second == null
        : second.equals(getClass().cast(o).second));
  }
  public int hashCode() {
    int result = 17;
    if(first != null) {
      result = result * 37 + first.hashCode();
    }
    return second == null ? result : result * 37 + second.hashCode();
  }
  public int compareTo(Object o) {
    if(o.getClass() != getClass()) { 
      throw new ClassCastException();
    }
    int comparation = first.compareTo(getClass().cast(o).first);
    return comparation != 0 
      ? comparation 
      : second.compareTo(getClass().cast(o).second);
  }
}

public class ThreeTuple<A extends Comparable, B extends Comparable, C extends Comparable>
    extends TwoTuple<A, B>  {
  public final C third;
  public ThreeTuple(A a, B b, C c) {
    super(a, b);
    third = c;
  }
  protected String toString2() {
    return super.toString2() + ", " + third;
  }
  public boolean equals(Object o) {
    return super.equals(o) && 
      (third == null 
        ? getClass().cast(o).third == null 
        : third.equals(getClass().cast(o).third));
  }
  @Override
  public int hashCode() {
    int result = super.hashCode();
    return third == null ? result : result * 37 + third.hashCode();
  }
  @Override
  public int compareTo(Object o) {
    int comparation = super.compareTo(o);
    return comparation != 0 
      ? comparation 
      : third.compareTo(getClass().cast(o).third);
  }
}
也许这是不遵守文件(?)的错误陈述

-- 当做
Pawel

最好尝试这样写:基类的实例和派生类的实例无法使用
compareTo
进行比较。这可能适用于许多场景,但这并不一定是作者所说的“同时正确实现”“。最好尝试这样编写:基类的实例和派生类的实例不能使用
compareTo
进行比较。这可能适用于许多场景,但并不一定是作者所说的“同时正确实现”。
TwoTuple(...) implements Comparable<TwoTuple> {
(...)
int compareTo(TwoTuple o) {
TwoTuple(...) implements Comparable {
(...)
 int compareTo(Object o) {