-
Notifications
You must be signed in to change notification settings - Fork 242
Description
Base class for Superheroes
class Superhero:
def init(self, name, power, weakness):
self.name = name
self.power = power
self.weakness = weakness
def display_info(self):
print(f"Name: {self.name}")
print(f"Power: {self.power}")
print(f"Weakness: {self.weakness}")
def action(self):
print(f"{self.name} is using their superpower: {self.power}!")
Inheritance to explore polymorphism
class FlyingHero(Superhero):
def init(self, name, power, weakness, flight_speed):
super().init(name, power, weakness)
self.flight_speed = flight_speed
def action(self):
print(f"{self.name} is flying at speed of {self.flight_speed}!")
class StrengthHero(Superhero):
def init(self, name, power, weakness, strength_level):
super().init(name, power, weakness)
self.strength_level = strength_level
def action(self):
print(f"{self.name} is showing their strength level of {self.strength_level}!")
Base class for animals
class Animal:
def move(self):
print("Animal is moving")
Inherited classes for specific animals
class Dog(Animal):
def move(self):
print("Dog is running 🐕")
class Bird(Animal):
def move(self):
print("Bird is flying 🕊️")
Base class for vehicles
class Vehicle:
def move(self):
print("Vehicle is moving")
Inherited classes for specific vehicles
class Car(Vehicle):
def move(self):
print("Car is driving 🚗")
class Plane(Vehicle):
def move(self):
print("Plane is flying
Creating objects for Superheroes
hero1 = Superhero("Captain Code", "Programming", "Bugs")
hero2 = FlyingHero("Sky Soarer", "Flight", "High Altitude", 900)
hero3 = StrengthHero("Mighty Muscle", "Super Strength", "Kryptonite", 100)
Display information and actions for Superheroes
print("\nSuperheroes:")
hero1.display_info()
hero1.action()
hero2.display_info()
hero2.action()
hero3.display_info()
hero3.action()
Creating objects for animals and vehicles
dog = Dog()
bird = Bird()
car = Car()
plane = Plane()
Demonstrate polymorphism
print("\nPolymorphism Challenge:")
for obj in (dog, bird, car, plane):
obj.move()