Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Python #133

Open
Kvinalpha opened this issue Dec 17, 2024 · 0 comments
Open

Python #133

Kvinalpha opened this issue Dec 17, 2024 · 0 comments

Comments

@Kvinalpha
Copy link

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()

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant