[英]How to use set methods on obj. reference to change values
package restaurantclient;
public class RestaurantClient {
public static void main(String[] args) {
Restaurant r1 = new Restaurant("McDonalds", 1000000, 8.00);
Restaurant r2 = new Restaurant("KFC", 500000, 6.00);
System.out.println(r1.toString());
System.out.println(r2.toString());
//I would like the code to go here, in-between the toString and the equals comparison.
if (r1.equals(r2)) {
System.out.println("The objects are equal.");
}
else {
System.out.println("The objects are not equal.");
System.out.println();
}
System.out.println();
System.out.print("The avg. annual taxes paid by restaurant 1 is: $");
System.out.println(r1.getAverageTaxes());
System.out.print("The avg. annual taxes paid by restaurant 2 is: $");
System.out.println(r2.getAverageTaxes());
System.out.println();
}
}
你好。 我一般對Java和OOP還是陌生的,但是在Python和C中還是可以通過的。 我需要找出如何在Restaurant對象引用r2上使用適當的mutator(setter)方法,並更改所服務的人數和在餐廳對象參考r1中,每人的平均價格達到相同的值。 我不想更改餐廳名稱。
餐廳類:
package restaurantclient;
public class Restaurant extends Store {
//Instance Variables
private int peopleServed;
private double averagePrice;
//Constructor with 3 parameters
public Restaurant(String storename, int peopleServed, double averagePrice) {
super(storename);
setPeopleServed(peopleServed);
setAveragePrice(averagePrice);
}
//Getters (Accessors)
public int getPeopleServed() {
return peopleServed;
}
public double getAveragePrice() {
return averagePrice;
}
//Setters (Mutators)
public void setPeopleServed(int peopleServed) {
this.peopleServed = peopleServed;
}
public void setAveragePrice(double averagePrice) {
this.averagePrice = averagePrice;
}
//toString Method [Must Override]
@Override
public String toString() {
String information = "Store name: " + (super.getName());
information += "\n" + "The number of people served: " + peopleServed;
information += "\n" + "The average price per person: $" + averagePrice;
return information;
}
//Equals Method
public boolean equals (Restaurant restaurant) {
if (this == restaurant)
return true;
if (restaurant == null)
return false;
if (getClass() != restaurant.getClass())
return false;
Restaurant other = (Restaurant) restaurant;
if (super.getName() == null) {
if (other.getName() != null)
return false;
} else if (super.getName().equals(other.getName()))
return false;
if (peopleServed == -1) {
if (other.peopleServed != -1)
return false;
} else if (peopleServed == (other.peopleServed))
return false;
if (averagePrice == -1) {
if (other.averagePrice != -1)
return false;
else if (averagePrice == (other.averagePrice))
return false;
}
return false;
}
public double getAverageTaxes() {
double total;
total = this.getPeopleServed() * this.getAveragePrice()
* super.CA_TAX_RATE;
return total;
}
}
r2.setAveragePrice(r1.getAveragePrice());
r2.setPeopleServed(r1.getPeopleServed());
您已經在該類上定義了setter方法-
//Setters (Mutators)
public void setPeopleServed(int peopleServed) {
this.peopleServed = peopleServed;
}
public void setAveragePrice(double averagePrice) {
this.averagePrice = averagePrice;
}
而且,關於您的問題-
在餐廳對象參考r2上使用適當的mutator(setter)方法,並將服務的人數和每人的平均價格更改為餐廳對象參考r1中的相同值
首先,使用getter方法檢索r1
對象的值,然后將這些值傳遞給r2
對象上的setter方法。 舉個例子 -
int peopleToSet = r1.getPeopleServed();
r2.setPeopleServed(peopleToSet);
或者,更簡而言之-
r2.setPeopleServed(r1.getPeopleServed());
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.