2 回答
TA贡献1806条经验 获得超5个赞
假设 'i' 是一个 Class 变量,您可以将方法定义为:
public double amount(Gamehouse g){
return i.pricePerHour() * getHowMuchUserWantHours();
}
并将其称为:
User user1 = new User(2, 14, amount(), "Mark", "John", "055-555-633");
TA贡献1812条经验 获得超5个赞
如果您已经将 2 ( getHowMuchUserWantHours) 发送到构造函数,请在构造函数中计算它。
public User(int hours, int unknown, int pricePerHour, String surname, String first name, String phone) {
this.amount = hours * pricePerHour;
// rest of fields
}
创建用户
User user = new User(2, 14, g.getPricePerHours(), "Mark", "John", "055-555-633");
如果 hours 也是 User 的成员,这是有道理的,如果不是并且它仅用于计算,则将其作为参数跳过
public User(int unknown, int amount, String surname, String first name, String phone) {
this.amount = amount;
// rest of fields
}
将 calcAmount 方法添加到 User 类
public static int calcAmount(int hour, int pricePerHour) {
return hour * pricePerHour;
}
并创建用户
User user = new User(14, calcAmount(2, 2* g.getPricePerHours()), "Mark", "John", "055-555-633");
添加回答
举报