蔬菜、水果、肉、水、食油、食盐、食糖、味精等,都提供了烹饪服务。请为之设计一个接口,并通过一些类实现的代码
时间: 2023-10-20 14:15:08 浏览: 39
接口设计如下:
```java
public interface CookingService {
void chopVegetables();
void peelFruits();
void cutMeat();
void pourWater();
void pourOil();
void addSalt();
void addSugar();
void addMSG();
}
```
通过实现类来实现这个接口:
```java
public class CookingServiceImpl implements CookingService {
@Override
public void chopVegetables() {
System.out.println("切蔬菜");
}
@Override
public void peelFruits() {
System.out.println("削水果皮");
}
@Override
public void cutMeat() {
System.out.println("切肉");
}
@Override
public void pourWater() {
System.out.println("倒水");
}
@Override
public void pourOil() {
System.out.println("倒油");
}
@Override
public void addSalt() {
System.out.println("加盐");
}
@Override
public void addSugar() {
System.out.println("加糖");
}
@Override
public void addMSG() {
System.out.println("加味精");
}
}
```
使用实现类来调用:
```java
public class Cook {
public static void main(String[] args) {
CookingService cookingService = new CookingServiceImpl();
cookingService.chopVegetables();
cookingService.peelFruits();
cookingService.cutMeat();
cookingService.pourWater();
cookingService.pourOil();
cookingService.addSalt();
cookingService.addSugar();
cookingService.addMSG();
}
}
```
相关推荐
![txt](https://img-home.csdnimg.cn/images/20210720083642.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![application/x-rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)