用java输出以下内容:润田翠,容量:500,价格:1 农夫山泉,容量:500,价格:2 农夫山泉,容量:500,价格:2 润田翠,容量:500,价格:1 应付金额:6
时间: 2023-07-02 15:13:34 浏览: 156
好的,以下是输出内容的Java代码:
```java
public class Test {
public static void main(String[] args) {
Beverage b1 = new Beverage("润田翠", 500, 1);
Beverage b2 = new Beverage("农夫山泉", 500, 2);
Beverage b3 = new Beverage("农夫山泉", 500, 2);
Beverage b4 = new Beverage("润田翠", 500, 1);
System.out.println(b1);
System.out.println(b2);
System.out.println(b3);
System.out.println(b4);
double total = b1.getPrice() + b2.getPrice() + b3.getPrice() + b4.getPrice();
System.out.println("应付金额:" + total);
}
}
class Beverage {
private String name;
private int capacity;
private double price;
public Beverage(String name, int capacity, double price) {
this.name = name;
this.capacity = capacity;
this.price = price;
}
public String getName() {
return name;
}
public int getCapacity() {
return capacity;
}
public double getPrice() {
return price;
}
public String toString() {
return name + ",容量:" + capacity + ",价格:" + price;
}
}
```
程序中,我们定义了一个饮料类 `Beverage`,它包含了饮料的名称、容量和价格。在测试类中,我们创建了四个饮料对象,并依次输出它们的信息。最后,我们计算出了应付金额并输出。
阅读全文