Java 错误是什么;“不能取消引用双精度”;什么意思?
我知道ArrayList不能保存任何原始数据 但是,如何将方法horseFeed()与Arraylist构造函数一起调用到驱动程序中,以避免出现双重解引用错误 还有人能给我解释一下什么是双解引用错误,为什么我会得到它,请帮忙 这个方法在我的课堂上Java 错误是什么;“不能取消引用双精度”;什么意思?,java,arraylist,compiler-errors,double,Java,Arraylist,Compiler Errors,Double,我知道ArrayList不能保存任何原始数据 但是,如何将方法horseFeed()与Arraylist构造函数一起调用到驱动程序中,以避免出现双重解引用错误 还有人能给我解释一下什么是双解引用错误,为什么我会得到它,请帮忙 这个方法在我的课堂上 public class horse { . . . //took out a lot of code as it was not important to the problem public String horseFee
public class horse
{
.
.
.
//took out a lot of code as it was not important to the problem
public String horseFeed(double w)
{
double sFeed= w*.015;
double eFeed= w*.013;
String range = sFeed + " < " + eFeed;
return range;
}
}
这是司机
public class Main
{
public static void main(String args[])
{
//returns the weight of the horse works fine
System.out.println(stable1.findHorseFeed(1));
// This is supposed to use the horseFeed method in the class by using the horse's weight. Where can i place the horseFeed method without getting an error?
System.out.println(stable1.findHorseFeed(1).horseFeed());
}
}
该错误意味着您试图对
double
值调用方法-在Java中,double
是一种基本类型,您无法对其调用方法:
stable1.findHorseFeed(1).horseFeed()
^ ^
returns a double can't call any method on it
您需要在正确的对象上调用该方法,并使用预期的参数-如下所示:
Horse aHorse = new Horse(...);
aHorse.horseFeed(stable1.findHorseFeed(1));
方法horseFeed()
位于Horse
类中,它接收类型为double
的参数,该参数由HorseStable
类中的方法findHorseFeed()
返回。显然,首先需要创建Horse
类型的实例来调用它
另外,请遵循类名以大写字符开头的约定。findHorseFeed()
返回一个double
,您不能在其上调用horseFeed()
方法(它返回一个double
,而不是horse
对象)
首先,您需要编写(并随后调用)另一个方法来返回一个horse对象,然后可以对该对象调用horseFeed()
在类horseStable
中,您将需要一个类似
public horse findtHorse(int i) {
horse myhorse = horseList.get(i);
return myhorse
}
您可能还可以在getHorse()
上重构findHorseFeed
到.getWeight()
:
然后你可以打电话:
horse myHorse = stable1.findHorse(1);
String range = myHorse.horseFeed(stable1.findHorseFeed(myHorse));
不清楚您在哪一行收到此错误。请删除那些与您的问题无关的代码。
public double findHorseFeed(horse myhorse) {
double weight = myhorse.getWeight();
return weight
}
horse myHorse = stable1.findHorse(1);
String range = myHorse.horseFeed(stable1.findHorseFeed(myHorse));