Dog with inheritance

This commit is contained in:
Michael Schären 2026-02-27 16:42:16 +01:00
parent 41c0c31f97
commit 54b982f637

59
src/dog.py Normal file
View File

@ -0,0 +1,59 @@
class Dog:
amount_of_dogs = 0
name: str
race: str
age: int
weight: float
bark_sound: str
def __init__(
self, name: str, race: str, age: int, weight: float, bark_sound: str
) -> None:
self.name = name
self.race = race
self.age = age
self.weight = weight
self.bark_sound = bark_sound
Dog.amount_of_dogs += 1
def __str__(self):
return f"{self.name} is a {self.age} year old {self.race}"
def bark(self, times: int = 1) -> str:
return f"{self.name} says:" + (f" {self.bark_sound}" * times)
def birthday(self) -> str:
self.age += 1
return f"Happy Birthday, {self.name}! You're now {self.age} years old!"
def is_puppy(self) -> bool:
return self.age < 2
class Shepperd(Dog):
race: str = "shepperd"
bark_sound: str = "Woo!"
def __init__(self, name: str, age: int, weight: float) -> None:
Dog.__init__(self, name, Shepperd.race, age, weight, Shepperd.bark_sound)
class Poodle(Dog):
race: str = "poodle"
bark_sound: str = "Wow!"
def __init__(self, name: str, age: int, weight: float) -> None:
Dog.__init__(self, name, Poodle.race, age, weight, Poodle.bark_sound)
dog1 = Shepperd("Bello", 5, 22.5)
print(dog1)
print(dog1.bark())
print(dog1.birthday())
print(Dog.amount_of_dogs)
dog2 = Poodle("Barky", 10, 20)
print(dog2.bark(3))
print(dog2.is_puppy())
print(Dog.amount_of_dogs)