构造一个coffee类作为product类的扩展类
时间: 2024-01-26 08:04:05 浏览: 30
class Coffee(Product):
def __init__(self, name, price, origin, roast_level):
super().__init__(name, price)
self.origin = origin
self.roast_level = roast_level
def get_origin(self):
return self.origin
def set_origin(self, origin):
self.origin = origin
def get_roast_level(self):
return self.roast_level
def set_roast_level(self, roast_level):
self.roast_level = roast_level
def __str__(self):
return f"{self.name}, {self.price}, {self.origin}, {self.roast_level}"
相关问题
建立一个java抽象类drink
好的,我可以帮您建立一个Java抽象类Drink。这个类可以作为所有饮料类的父类,包含一些通用的属性和方法。下面是一个简单的实现:
```java
public abstract class Drink {
protected String name;
protected double price;
public Drink(String name, double price) {
this.name = name;
this.price = price;
}
public String getName() {
return this.name;
}
public double getPrice() {
return this.price;
}
public abstract void taste();
}
```
在这个类中,我们定义了两个属性,name和price,以及一个构造函数和两个getter方法。此外,我们定义了一个抽象方法taste(),这个方法在子类中必须被实现。
这个抽象类可以作为所有饮料类的父类,例如:
```java
public class Coffee extends Drink {
public Coffee(String name, double price) {
super(name, price);
}
public void taste() {
System.out.println("This coffee tastes great!");
}
}
public class Tea extends Drink {
public Tea(String name, double price) {
super(name, price);
}
public void taste() {
System.out.println("This tea tastes good!");
}
}
```
在Coffee和Tea类中,我们继承了Drink类,并实现了它的抽象方法taste()。这样,我们就可以方便地定义各种饮料类,并且统一地使用它们的属性和方法。
Python用类实现咖啡机
以下是使用Python类实现咖啡机的示例代码:
```python
class CoffeeMachine:
def __init__(self, water, milk, coffee_beans, cups, money):
self.water = water
self.milk = milk
self.coffee_beans = coffee_beans
self.cups = cups
self.money = money
def make_coffee(self, water_needed, milk_needed, beans_needed, cost):
if self.water < water_needed:
print("Sorry, not enough water!")
elif self.milk < milk_needed:
print("Sorry, not enough milk!")
elif self.coffee_beans < beans_needed:
print("Sorry, not enough coffee beans!")
elif self.cups < 1:
print("Sorry, not enough cups!")
else:
print("I have enough resources, making you a coffee!")
self.water -= water_needed
self.milk -= milk_needed
self.coffee_beans -= beans_needed
self.cups -= 1
self.money += cost
def buy(self):
choice = input("What do you want to buy? 1 - espresso, 2 - latte, 3 - cappuccino: ")
if choice == "1":
self.make_coffee(250, 0, 16, 4)
elif choice == "2":
self.make_coffee(350, 75, 20, 7)
elif choice == "3":
self.make_coffee(200, 100, 12, 6)
else:
print("Invalid choice!")
def fill(self):
self.water += int(input("How many ml of water do you want to add? "))
self.milk += int(input("How many ml of milk do you want to add? "))
self.coffee_beans += int(input("How many grams of coffee beans do you want to add? "))
self.cups += int(input("How many disposable cups do you want to add? "))
def take(self):
print("I gave you $", self.money)
self.money = 0
def remaining(self):
print("The coffee machine has:")
print(self.water, "of water")
print(self.milk, "of milk")
print(self.coffee_beans, "of coffee beans")
print(self.cups, "of disposable cups")
print("$", self.money, "of money")
# 创建一个咖啡机对象
my_machine = CoffeeMachine(400, 540, 120, 9, 550)
# 与用户交互
while True:
action = input("What would you like to do? (buy, fill, take, remaining, exit): ")
if action == "buy":
my_machine.buy()
elif action == "fill":
my_machine.fill()
elif action == "take":
my_machine.take()
elif action == "remaining":
my_machine.remaining()
elif action == "exit":
break
else:
print("Invalid action!")
```
该代码创建了一个名为`CoffeeMachine`的类,该类具有制作咖啡、购买咖啡、填充咖啡机、取走咖啡机中的钱和显示咖啡机状态等方法。在类的构造函数中,我们初始化了咖啡机的水、牛奶、咖啡豆、杯子和钱的数量。然后,我们使用一个无限循环与用户交互,直到用户选择退出。
相关推荐
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![txt](https://img-home.csdnimg.cn/images/20210720083642.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.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)
![](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)
![](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)
![](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)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)