Python MULTIPLE INHERITANCE is easy! 🐟

preview_player
Показать описание
00:00:00 multiple inheritance
00:03:29 multilevel inheritance
00:05:03 attributes

class Animal:

def __init__(self, name):

def eat(self):

def sleep(self):

class Prey(Animal):
def flee(self):

class Predator(Animal):
def hunt(self):

class Rabbit(Prey):
pass

class Hawk(Predator):
pass

class Fish(Prey, Predator):
pass

rabbit = Rabbit("Bugs")
hawk = Hawk("Tony")
fish = Fish("Nemo")
Рекомендации по теме
Комментарии
Автор

# multiple inheritance = inherit from more than one parent class
# C(A, B)

# multilevel inheritance = inherit from a parent which inherits from another parent
# C(B) <- B(A) <- A

class Animal:

def __init__(self, name):
self.name = name

def eat(self):
print(f"{self.name} is eating")

def sleep(self):
print(f"{self.name} is sleeping")

class Prey(Animal):
def flee(self):
print(f"{self.name} is fleeing")

class Predator(Animal):
def hunt(self):
print(f"{self.name} is hunting")

class Rabbit(Prey):
pass

class Hawk(Predator):
pass

class Fish(Prey, Predator):
pass

rabbit = Rabbit("Bugs")
hawk = Hawk("Tony")
fish = Fish("Nemo")

BroCodez
Автор

Dude, you have to get into Software design patterns using Python and OOP, please

ReighKnight