Java 为什么这个mutator方法在调用它之后仍然返回错误?
我从一个“Holiday”类调用了“setPrice()”mutator方法,该类将“double”作为数据类型。但是,当我添加'holiday.setPrice()'时,它说“holiday类中的方法setPrice不能应用于给定的类型:double”。我不明白这是为什么Java 为什么这个mutator方法在调用它之后仍然返回错误?,java,bluej,Java,Bluej,我从一个“Holiday”类调用了“setPrice()”mutator方法,该类将“double”作为数据类型。但是,当我添加'holiday.setPrice()'时,它说“holiday类中的方法setPrice不能应用于给定的类型:double”。我不明白这是为什么 public void checkOut(Member member, Holiday holiday){ if(member.getBalance() >= holiday.getPrice()){
public void checkOut(Member member, Holiday holiday){
if(member.getBalance() >= holiday.getPrice()){
System.out.println("Transaction complete");
System.out.println("You have been automatically logged off");
member.setLoginStatus(false);
if(checkHitDiscount() == true){
holiday.setPrice() = discount - holiday.getPrice() ;
System.out.println(" Good news! You're eligible for a discount!");
}else{
System.out.println("No discount available, try again next time");
}
} else {
System.out.println("You don't have suffiencient funds");
}
}
您正在尝试使用以下行:
holiday.setPrice() = discount - holiday.getPrice();
这应该是
holiday.setPrice(discount - holiday.getPrice());
相反。函数的输入在括号内
编辑:discount
和holiday.getPrice()
也应该根据它们的名称进行反转
EDIT2:您现有的代码试图做的是将折扣-holiday.getPrice()的值赋值给double,double由假设的
holiday.setPrice()返回,没有输入。这是不允许的,因为这是一个值,而不是一个变量(即:一个名称,指内存中存储值的位置)。这有点像试图执行5=3+4
请尝试:holiday.setPrice(折扣-holiday.getPrice())代码>您无法为返回值分配某些内容,也可能应该是holiday。getPrice()-折扣?现在更有意义了。欣赏help@ThaboStephaniousEberhart那么,对于以后来的人,你可能应该将答案标记为已接受。