[英]I am having problems with compareTo
好吧,我需要创建一个有两个接口的项目,并且它们都在两个不相关的类中使用。 除了compareTo方法,我设法使其他所有东西都能正常工作。 我开设的两个课程是汽车和马。 我想做的是比较从Horse出发的MilesGoal到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类别几乎相同,我需要找到一种方法来比较他们的两个里程。Goal以查看哪个更大。 我尝试了多种方法,但似乎不起作用
这是我制作的界面:
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)
另外,使用适当的大小写(变量使用小写字母,clases / interfaces使用大写字母)以及setter和getter。
此外,您可能需要转换为接口,以便可以将方法与实现接口的其他类一起使用
if (milesGoal > ((MilesInterface) other).milesGoal)
首先, (Horse)milesGoal.Other
应该是((Horse) Other).milesGoal
。
我建议使用一种与Horse
进行比较的方法和一种与Car
进行比较的方法来重载compareTo
。 然后你的代码看起来像
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;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.