用java实现Beverage、Tea、Coffee、Beer类并给出简单的测试
时间: 2023-09-19 16:08:27 浏览: 75
使用java编写的测试类
Beverage类:
```
public abstract class Beverage {
protected String description;
public String getDescription() {
return description;
}
public abstract double cost();
}
```
Tea类:
```
public class Tea extends Beverage {
public Tea() {
description = "Tea";
}
public double cost() {
return 1.5;
}
}
```
Coffee类:
```
public class Coffee extends Beverage {
public Coffee() {
description = "Coffee";
}
public double cost() {
return 2.0;
}
}
```
Beer类:
```
public class Beer extends Beverage {
public Beer() {
description = "Beer";
}
public double cost() {
return 3.5;
}
}
```
测试:
```
public class Test {
public static void main(String[] args) {
Beverage tea = new Tea();
System.out.println(tea.getDescription() + " cost: " + tea.cost());
Beverage coffee = new Coffee();
System.out.println(coffee.getDescription() + " cost: " + coffee.cost());
Beverage beer = new Beer();
System.out.println(beer.getDescription() + " cost: " + beer.cost());
}
}
```
输出:
```
Tea cost: 1.5
Coffee cost: 2.0
Beer cost: 3.5
```
阅读全文