Files
PPE2/src/exercises/dog.py
T
2026-03-12 15:07:47 +01:00

75 lines
1.8 KiB
Python

from abc import ABC, abstractmethod
class Dog(ABC):
amount_of_dogs = 0
name: str
age: int
weight: float
bark_sound: str
def __init__(self, name: str, age: int, weight: float, bark_sound: str) -> None:
self.name = name
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.__class__.__name__}"
@abstractmethod
def bark(self, times: int = 1) -> str:
pass
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
def __eq__(self, other):
return self.name == other.name
class Shepperd(Dog):
bark_sound: str = "Woo!"
def __init__(self, name: str, age: int, weight: float) -> None:
Dog.__init__(self, name, age, weight, Shepperd.bark_sound)
def bark(self, times: int = 1) -> str:
return f"{self.name} says:" + (f" {self.bark_sound}" * times)
class Poodle(Dog):
bark_sound: str = "Wow!"
def __init__(self, name: str, age: int, weight: float) -> None:
Dog.__init__(self, name, age, weight, Poodle.bark_sound)
def bark(self, times: int = 1) -> str:
return f"{self.name} says:" + (f" {self.bark_sound}" * times)
class Bulldog(Dog):
bark_sound: str = "WUUUU!"
def __init__(self, name: str, age: int, weight: float) -> None:
Dog.__init__(self, name, age, weight, Bulldog.bark_sound)
dog1 = Shepperd("Bello", 5, 22.5)
print(dog1)
print(dog1.bark())
print(dog1.birthday())
print(Dog.amount_of_dogs)
dog2 = Poodle("Bello", 10, 20)
print(dog2.bark(3))
print(dog2.is_puppy())
print(Dog.amount_of_dogs)
print(dog2 == dog1)