** class New class name (original class name): **
from menu_item import MenuItem
class Food(MenuItem):
pass
Description You can overwrite the contents by defining a method with the same name as the parent class in the child class. When a method is called, the overwritten method is called.
** Example sentence ** Partial excerpt of the method part (overriding the info method) Parent class (already has a method)
menu_item.py
class MenuItem:
def info(self):
return self.name + ': ¥' + str(self.price)
Override in child class
food.py
from menu_item import MenuItem
class Food(MenuItem):
def info(self):
return self.name + ': ¥' + str(self.price) + ' (' + str(self.calorie) + 'kcal)'
Recommended Posts