java创建一个饮料类,其中包含私有属性名称、价格和容量,然 后创建方法去访问和修改这些属性的测试类
时间: 2024-05-11 08:18:42 浏览: 71
java的测试类
4星 · 用户满意度95%
下面是Java代码实现:
```java
public class Beverage {
private String name;
private double price;
private double capacity;
public Beverage(String name, double price, double capacity) {
this.name = name;
this.price = price;
this.capacity = capacity;
}
public String getName() {
return name;
}
public double getPrice() {
return price;
}
public double getCapacity() {
return capacity;
}
public void setName(String name) {
this.name = name;
}
public void setPrice(double price) {
this.price = price;
}
public void setCapacity(double capacity) {
this.capacity = capacity;
}
}
```
测试类:
```java
public class BeverageTest {
public static void main(String[] args) {
Beverage beverage = new Beverage("可乐", 3.0, 500.0);
System.out.println(beverage.getName() + " " + beverage.getPrice() + "元 " + beverage.getCapacity() + "毫升");
beverage.setName("雪碧");
beverage.setPrice(3.5);
beverage.setCapacity(600.0);
System.out.println(beverage.getName() + " " + beverage.getPrice() + "元 " + beverage.getCapacity() + "毫升");
}
}
```
输出结果:
```
可乐 3.0元 500.0毫升
雪碧 3.5元 600.0毫升
```
阅读全文