From b7c9cfb84e80feb2c48c61e780674b40f184db85 Mon Sep 17 00:00:00 2001 From: schaermicha1 Date: Fri, 27 Feb 2026 16:42:16 +0100 Subject: [PATCH] Dog with inheritance --- src/dog.py | 59 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 59 insertions(+) create mode 100644 src/dog.py diff --git a/src/dog.py b/src/dog.py new file mode 100644 index 0000000..257c1c4 --- /dev/null +++ b/src/dog.py @@ -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) -- 2.30.2