gpt4 book ai didi

java - 我在使用 CompareTo 时遇到问题

转载 作者:行者123 更新时间:2023-12-01 14:44:19 25 4
gpt4 key购买 nike

好吧,我需要创建一个项目,其中有两个接口(interface),并且它们都在两个不相关的类中使用。除了compareTo 方法之外,我设法使其他所有内容都能正常运行。我制作的两个类(class)是汽车和马。我想做的是将 Horse 中的 MilesGoal 与 Car 中的 MilesGoal 进行比较,然后返回 1、0 或 -1。

但是,当我尝试这样做时,我收到错误“无法取消引用 double ”

我已经在这个问题上停留了一段时间,试图找到不同的方法来处理这部分代码。我尝试在测试器类中使用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;
}
}

汽车类别与马类别几乎相同,我需要找到一种方法来比较它们的英里目标,看看哪一个更大。我尝试了多种方法,但似乎不起作用

这是我做的界面:

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);
}

最佳答案

首先,您正在编写attribute.object。这就是失败的原因。 Other.milesGoal 是一个更好的选择。

另一个问题是选角。您正在做的是将 milesGoal.other 转换为 Horse (您想要转换 milesGoal)

你应该使用

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

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

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

       if (milesGoal > ((MilesInterface) other).milesGoal)

关于java - 我在使用 CompareTo 时遇到问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15602680/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com