Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from abc import ABC, abstractmethod
- class Animal(ABC):
- def sound(self):
- pass
- def common_behavior(self):
- print("All animals need food and water")
- class Dog(Animal):
- def sound(self):
- return "Bark"
- class Cat(Animal):
- def sound(self):
- return "Meow"
- dog = Dog()
- cat = Cat()
- print(f"Dog says: {dog.sound()}")
- print(f"Cat says: {cat.sound()}")
- class Vehicle(ABC):
- @abstractmethod
- def start_engine(self):
- pass
- def stop_engine(self):
- pass
- class Car(Vehicle):
- def start_engine(self):
- print("Car engine started")
- def stop_engine(self):
- print("Car engine stopped")
- class Bike(Vehicle):
- def start_engine(self):
- print("Bike engine started")
- def stop_engine(self):
- print("Bike engine stopped")
- car = Car()
- bike = Bike()
- car.start_engine()
- car.stop_engine()
- bike.start_engine()
- bike.stop_engine()
- from abc import ABC, abstractmethod
- class Shape(ABC):
- @abstractmethod
- def area(self):
- pass
- class Square(Shape):
- def __init__(self, side_length):
- self.side_length = side_length
- def area(self):
- return self.side_length ** 2
- class Rectangle(Shape):
- def __init__(self, width, height):
- self.width = width
- self.height = height
- def area(self):
- return self.width * self.height
- square = Square(4)
- rectangle = Rectangle(4, 6)
- print(f"Area of Square: {square.area()}")
- print(f"Area of Rectangle: {rectangle.area()}")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement