Introduction à la Programmation Orientée Objet en Python
class Animal:
def __init__(self, name, age):
self.name = name
self.age = age
def speak(self):
raise NotImplementedError("Subclass must implement abstract method")
class Dog(Animal):
def speak(self):
return f"{self.name} says Woof!"
class Cat(Animal):
def speak(self):
return f"{self.name} says Meow!"
# Utilisation des classes
my_dog = Dog("Buddy", 3)
my_cat = Cat("Whiskers", 2)
print(my_dog.speak()) # Output: Buddy says Woof!
print(my_cat.speak()) # Output: Whiskers says Meow!