Worksheet 8 - OOP Class Dog example
Jump to navigation
Jump to search
Here’s an engaging OOP example in Python centered around animals, specifically a Dog class.
Dog Class Example
class Dog:
def __init__(self, name, breed, age):
self.name = name # Name of the dog
self.breed = breed # Breed of the dog
self.age = age # Age of the dog
def bark(self):
return f"{self.name} says Woof!"
def fetch(self, item):
return f"{self.name} is fetching the {item}!"
def introduce(self):
return f"This is {self.name}, a {self.age}-year-old {self.breed}."
# Creating an instance of the Dog class
my_dog = Dog("Buddy", "Golden Retriever", 3)
# Using the methods
print(my_dog.introduce()) # Output: This is Buddy, a 3-year-old Golden Retriever.
print(my_dog.bark()) # Output: Buddy says Woof!
print(my_dog.fetch("ball")) # Output: Buddy is fetching the ball!
Explanation:
- Class Definition: The Dog class has an __init__ method that initializes the dog's attributes: name, breed, and age.
- Methods:
- bark: This method simulates the dog's bark.
- fetch: This method allows the dog to "fetch" an item specified by the user.
- introduce: This method provides a friendly introduction of the dog.
Challenge 1: Create Additional Methods
Task: Add sit() and roll_over() methods to the Dog class.
class Dog:
def __init__(self, name, breed, age):
self.name = name # Name of the dog
self.breed = breed # Breed of the dog
self.age = age # Age of the dog
def bark(self):
return f"{self.name} says Woof!"
def fetch(self, item):
return f"{self.name} is fetching the {item}!"
def introduce(self):
return f"This is {self.name}, a {self.age}-year-old {self.breed}."
# New method for sitting
def sit(self):
return f"{self.name} sits down!"
# New method for rolling over
def roll_over(self):
return f"{self.name} rolls over!"
# Creating an instance of the Dog class
my_dog = Dog("Buddy", "Golden Retriever", 3)
# Using the new methods
print(my_dog.sit()) # Output: Buddy sits down!
print(my_dog.roll_over()) # Output: Buddy rolls over!
Annotations
- The sit() method returns a string indicating that the dog has sat down.
- The roll_over() method returns a string indicating that the dog has rolled over.
- You can test these methods just like the others by calling them on your my_dog instance.
Challenge 2: Expand the Class with a DogPark
Task: Create a DogPark class where multiple dogs can interact.
class Dog:
def __init__(self, name, breed, age):
self.name = name
self.breed = breed
self.age = age
def bark(self):
return f"{self.name} says Woof!"
class DogPark:
def __init__(self):
self.dogs = [] # List to hold the dogs in the park
def add_dog(self, dog):
self.dogs.append(dog) # Add a dog to the park
return f"{dog.name} has entered the park!"
def dog_bark_all(self):
# Make all dogs bark in the park
return [dog.bark() for dog in self.dogs]
# Creating instances of the Dog class
dog1 = Dog("Buddy", "Golden Retriever", 3)
dog2 = Dog("Max", "Beagle", 2)
# Creating an instance of DogPark
park = DogPark()
# Adding dogs to the park
print(park.add_dog(dog1)) # Output: Buddy has entered the park!
print(park.add_dog(dog2)) # Output: Max has entered the park!
# Making all dogs bark
barks = park.dog_bark_all()
for bark in barks:
print(bark) # Output: Buddy says Woof! and Max says Woof!
Annotations
- The DogPark class has a list called dogs to store the dogs in the park.
- The add_dog() method allows you to add a new dog to the park and returns a message confirming the action.
- The dog_bark_all() method makes all dogs in the park bark and returns their barks as a list.