feat: animal.py tutorial for Interface and Inheritance

This commit is contained in:
Sandro Zimmermann 2026-03-05 12:26:46 +01:00
parent b67ebcdcca
commit e18a3f062f

View 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))