feat: animal.py tutorial for Interface and Inheritance
This commit is contained in:
parent
b67ebcdcca
commit
e18a3f062f
32
src/tutorial/class/animal.py
Normal file
32
src/tutorial/class/animal.py
Normal file
@ -0,0 +1,32 @@
|
||||
from abc import ABC, abstractmethod
|
||||
|
||||
|
||||
class Animal(ABC):
|
||||
@abstractmethod
|
||||
def speak(slef) -> str:
|
||||
return "..."
|
||||
|
||||
def chorus(self, n: int) -> str:
|
||||
# Wiederhole den Laut n-mal als eine Zeile"
|
||||
if n <= 0:
|
||||
raise ValueError("n muss positiv sein")
|
||||
return " ".join(self.speak() for _ in range(n))
|
||||
|
||||
|
||||
class Dog(Animal):
|
||||
def speak(self) -> str:
|
||||
return "wuff"
|
||||
|
||||
|
||||
class Cat(Animal):
|
||||
def speak(self) -> str:
|
||||
return "miau"
|
||||
|
||||
|
||||
class Cow(Animal):
|
||||
def speak(self) -> str:
|
||||
return "muh"
|
||||
|
||||
|
||||
cow = Cow()
|
||||
print(cow.chorus(3))
|
||||
Loading…
x
Reference in New Issue
Block a user