如何实现 Comparable 接口来比较 2 个对象?

How do I implement the Comparable interface to compare 2 objects?

我正在尝试添加到这个程序中,以便我可以使用 Comparable 接口比较两个有理数(稍后将在驱动程序 class 中定义)。然而,当我制作 public int compareTo 时,它不允许我声明超过 1 个有理对象。我该怎么做?

public class Rational implements Comparable <Rational, Rational>
{ 
     private int numerator; 
     private int denominator; 

public Rational (int numer, int denom) 
{ // begins block
  if (denom == 0) 
  denom = 1; 

if (denom < 0)
{ 
  numer = numer * -1; 
  denom = denom * -1; 
} 
numerator = numer; 
denominator = denom; 
reduce();
}

public int getNumerator ()
{ 
  return numerator; 
} 
public int getDenominator ()
{ // begins block
return denominator;  
} 
public Rational reciprocal ()  
{ // begins block
return new Rational (denominator, numerator); 
} 
public Rational add (Rational op2) 
{ // begins block
int commonDenominator = denominator * op2.getDenominator();
int numerator1 = numerator * op2.getDenominator();
int numerator2 = op2.getNumerator() * denominator; 
int sum = numerator1 + numerator2; 
return new Rational (sum, commonDenominator); 
} 
public Rational subtract (Rational op2)
{ // begins block
int commonDenominator = denominator * op2.getDenominator(); 
int numerator1 = numerator * op2.getDenominator(); 
int numerator2 = op2.getNumerator() * denominator; 
int difference = numerator1 - numerator2;
return new Rational (difference, commonDenominator); 
} 
public Rational multiply (Rational op2) 
{ // begins block
int numer = numerator * op2.getNumerator(); 
int denom = denominator * op2.getDenominator(); 
return new Rational (numer, denom); 
} 

public Rational divide (Rational op2)
{ // begins block
return multiply (op2.reciprocal()); 
}
public boolean equals (Rational op2) 
{ // begins block
return ( numerator == op2.getNumerator() && denominator ==      op2.getDenominator() ); 
}
public String toString () 
{ // begins block
String result; 
if (numerator == 0) 
  result = "0"; 
else 
  if (denominator == 1) 
  result = numerator + ""; 
else 
  result = numerator + "/" + denominator; 
return result; 
}
private void reduce () 
{ // begins block
if (numerator != 0) 
{ // begins block
  int common = gcd  
    (Math.abs(numerator), denominator);
     numerator = numerator / common; 
     denominator = denominator / common; 
} // ends block
} 
private int gcd (int num1, int num2) 
{
while (num1 != num2) 
  if (num1 > num2) 
  num1 = num1 - num2; 
else 
  num2 = num2 - num1; 
return num1; 
} 

public int compareTo (Rational r1, Rational r2)
{ 

Float value1 = new Float ((float)r1.getNumerator() / r1.getDenominator()); 
Float value2 = new Float ((float)r2.getNumerator() / r2.getDenominator());  

int answer = value1.compareTo(value2);
return answer;

}
} 

Comparable 接口的 compareTo 方法只需要一个参数:要比较的 other 实例。 所以它只需要一个类型参数。 要修复您的 class 声明:

public class Rational implements Comparable<Rational>

按照您当前 compareTo 实施中的模式, 经过一些小的修正和改进, 这是实现 ComparablecompareTo 的一种方法:

  @Override
  public int compareTo(Rational o) {
    float mine = (float) getNumerator() / getDenominator();
    float other = (float) o.getNumerator() / o.getDenominator();
    return Float.compare(mine, other);
  }