java - java的compareTo()方法表现得很奇怪

标签 java object comparison compareto

嗨,我无法让它工作,我在对象比较中遇到错误...我如何将英寸转换为字符串(我从未使用过与字符串以外的任何内容进行比较),或者使用比较运算符比较整数,

Object comparison = this.inches.compareTo(obj.inches);

这是我到目前为止的代码

import java.io.*;
import java.util.*;
import java.lang.Integer;
import java.lang.reflect.Array;

 public class Distance implements Comparable<Distance> {

private static final String HashCodeUtil = null;
private int feet;
private int inches; 
private final int DEFAULT_FT = 1;
private final int DEFAULT_IN = 1;

public Distance(){
    feet = DEFAULT_FT;
    inches = DEFAULT_IN;
}
public Distance(int ft, int in){
    feet = ft;
    inches = in;
}

public void setFeet(int ft){
    try {
        if(ft<0){
            throw new CustomException("Distance is not negative");

        }

    }
    catch(CustomException c){
        System.err.println(c);
        feet =ft;
    }
}

public int getFeet(){
    return feet;
}

public void setInches(int in){

    try
    {
        if (in<0)
            throw new CustomException("Distance is not negative");
        //inches = in;
    }
    catch(CustomException c)
    {
        System.err.println(c);
        inches = in;
    }

}

public int getInches(){
    return inches;
}


public String toString (){
    return "<" + feet + ":" + inches + ">";

}

public Distance add(Distance m){
    Distance n = new Distance();
    n.inches = this.inches + m.inches;
    n.feet = this.feet + m.feet;

    while(n.inches>12){
        n.inches = n.inches - 12;
        n.feet++;
    }

    return n;
}

public Distance subtract(Distance f){
    Distance m = new Distance();
    m.inches = this.inches - f.inches;
    m.feet = this.feet - f.feet;
    while(m.inches<0){
        m.inches = m.inches - 12;
        feet--;

    }

    return m;
}


@Override
public int compareTo(Distance obj) {
    // TODO Auto-generated method stub

    final int BEFORE = -1;
    final int EQUAL = 0;
    final int AFTER = 1;

    if (this == obj) return EQUAL;

    if(this.DEFAULT_IN < obj.DEFAULT_FT) return BEFORE;
    if(this.DEFAULT_IN > obj.DEFAULT_FT) return AFTER;

    Object comparison = this.inches.compareTo(obj.inches);
    if (this.inches == obj.inches) return compareTo(null);

    assert this.equals(obj) : "compareTo inconsistent with equals";

    return EQUAL;

}

@Override public boolean equals( Object obj){
    if (obj != null) return false;
    if (!(obj intanceof Distance)) return false;

    Distance that = (Distance)obj;

            ( this.feet == that.feet &&
            this.inches == that.inches);
            return true;
            else
            return false;
}


 @Override public int hashCode(int, int) {
    int result = HashCodeUtil.inches;
    result = HashCodeUtil.hash(result, inches );
    result = HashCodeUtil.hash(result, feet);
    ruturn result;
   }

最佳答案

您正在比较对象引用。尝试比较对象的值(value);重写 hashCode() 或比较字段值。

@Override
public int compareTo(Distance obj) {
    ....
    if (this == obj) return EQUAL; <--- This
    ...
}

关于java - java的compareTo()方法表现得很奇怪,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12380583/

相关文章:

java - IIB - 使用 Java 计算节点将 BLOB 转换为字符串

javascript - 如何在Javascript中将两个数组添加到一个数组中

performance - 如何检查两个日期系列是否重叠并确定它出现的第一个日期?

c++ - 重载比较运算符失败 [C++11]

c++ - 如何在 C++ 中比较(目录)路径?

java - Tomcat:JSP 无法识别我的类文件

java模拟器简单的赛马游戏仅使用数组和循环

java - 从 servlet 连接到数据库

python - 向 python 添加嵌套属性

java - 将 Arraylist 与抽象对象转换