0

好吧,我需要创建一个项目,其中有两个接口,它们都用于两个不相关的类。除了 compareTo 方法之外,我设法让其他所有东西都能正常工作。我制作的两个课程是汽车和马。我想要做的是比较从 Horse 到 Car 的 milesGoal 并返回 1、0 或 -1。

但是,当我尝试这样做时,出现错误“无法取消引用双精度”

我一直坚持这一点,试图找到不同的方法来处理这部分代码。我尝试在测试器类中使用 compareTo 而不是创建一个方法,但我得到了同样的错误,我需要将它变成一个方法。

这是马类:

public class Horse implements milesInterface , kilometersInterface{
private double currentMile;
private double currentKilo;
private double milesGoal;
private double kilosGoal;
private String horseBreed;

// CONSTRUCTORS
public Horse(){
    currentMile = 0;
    currentKilo = 0;
    milesGoal = 0;
    kilosGoal = 0;
    horseBreed = "Unspecified";
}
public Horse(double cm, double ck, double mg, double kg, String hb){
    currentMile = cm;
    currentKilo = ck;
    milesGoal = mg;
    kilosGoal = kg;
    horseBreed = hb;
}

// MILE METHODS
public double remainingMiles(){ // Finds the remaining miles
    return milesGoal-currentMile;
}
public void halfMile(){ // Divides the desired goal halfway (Miles)
    milesGoal = milesGoal/2;
} 
public void setMileGoal(double newMile){ // Allows you to set a new goal
    milesGoal = newMile;
}
public double getMileGoal(){
    return milesGoal;
}

// KILOMETER METHODS
public double remainingKilos(){ // Finds the remaining Kilos
return kilosGoal-currentKilo;
}
public void halfKilo(){ // Divides the desire goal halfway (Kilos)
    kilosGoal = kilosGoal/2;
}
public void setKiloGoal(){ // Allows you to set a new goal
    kilosGoal = milesGoal*1.6;
}
public void setCurrentKilo(){ // Allows you to set the current Kilo
    currentKilo = currentMile * 1.6;
}

// UNIQUE METHODS
public double getMilesStatus(){
    return currentMile;
}
public double getKilosStatus(){
    return currentKilo;
}
public String getHorseBreed(){
    return horseBreed;
}
public void convertToKilos(){ // Converts current miles to kilometers
    double kilos = currentMile * 1.6;
    System.out.println("The current miles to kilometers is: " + kilos + "km.");
}
public void convertToMiles(){ // Converts current kilometers to miles
    double miles = currentKilo * .6;
    System.out.println("The current kilometers to miles is: " + miles + "m.");
}
public void milesPerHour(double hours){ // Calculates the mph to the goal by a time
double answer = milesGoal / hours;
System.out.println("The mph needed to reach the desination in " + hours + " hours: " + answer);
}
 public void kilosPerHour(double hours){ // Calculates the kmph to the goal by a time
double answer = kilosGoal / hours;
System.out.println("The kilometers needed to reach the desination in " + hours + " hours: " + answer);
}
public int compareTo(Object Other){

    if(milesGoal > (Horse)milesGoal.Other)
        return 1;
    if(milesGoal < (Horse)milesGoal.Other)
        return 0;
        return -1;
    }
 }

Car 类与 Horse 类几乎相同,我需要找到一种方法来比较它们的两个 milesGoal 以查看哪个更大。我尝试了多种方法,但似乎不起作用

这是我做的界面:

abstract interface milesInterface{
public double remainingMiles();
public void halfMile();
public void setMileGoal(double newMile);
public int compareTo(Object Other);
}
abstract interface kilometersInterface{
public double remainingKilos();
public void halfKilo();
public void setCurrentKilo();
public int compareTo(Object Other);
}
4

2 回答 2

1

首先,你在写attribute.object. 这就是失败的原因。Other.milesGoal是更好的选择。

另一个问题是铸造。你正在做的是试图投到milesGoal.otherHorse你想投milesGoal

你应该使用

       if (milesGoal > ((Horse) other).milesGoal)

此外,使用正确的大小写(变量小写,类/接口大写)和 setter 和 getter。

此外,您可能希望转换为接口,以便可以将这些方法与实现它的其他类一起使用

       if (milesGoal > ((MilesInterface) other).milesGoal)
于 2013-03-24T19:08:20.287 回答
0

首先,(Horse)milesGoal.Other应该是((Horse) Other).milesGoal

我建议compareTo使用一种与s 进行比较Horse的方法和一种与 s 进行比较的方法重载Car。然后你的代码看起来像

public int compareTo(Horse horse){
    if(milesGoal > horse.milesGoal)
        return 1;
    if(milesGoal < horse.milesGoal)
        return -1;
    return 0;
}

public int compareTo(Car car){
    if(milesGoal > car.milesGoal)
        return 1;
    if(milesGoal < car.milesGoal)
        return -1;
    return 0;
}
于 2013-03-24T19:11:25.720 回答