如何在obj上使用set方法。参考更改值

时间:2017-02-06 03:45:22

标签: java setter

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)方法,并更改number的值服务的人和每人的平均价格在餐厅对象参考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; 
} 
}

2 个答案:

答案 0 :(得分:2)

TestClass::TestMethod

答案 1 :(得分:0)

您已经在类上定义了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());