Advertisement
fkudinov

13. OOP: First Look at Class and Object

Oct 13th, 2023
95
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.16 KB | Source Code | 0 0
  1. class Car:
  2.     wheels = "four"
  3.     doors = 4
  4.     working_engine = False
  5.    
  6.     engine_start_msg = "Engine has started"
  7.     engine_stop_msg = "Engine has stopped"
  8.    
  9.     def __init__(self, brand: str = "", model: str = ""):
  10.         self.brand = brand
  11.         self.model = model
  12.        
  13.     def start_engine(self):  
  14.         self.working_engine = True
  15.         print(self.engine_start_msg)
  16.    
  17.     def stop_engine(self):  
  18.         self.working_engine = False
  19.         print(self.engine_stop_msg)
  20.  
  21.  
  22. class ElectricCar(Car):
  23.  
  24.     engine_start_msg = "Electric motor has started"
  25.     engine_stop_msg = "Electric motor has stopped"
  26.    
  27.     def __init__(self, battery_capacity: int, brand: str = "", model: str = ""):
  28.         super().__init__(brand, model)
  29.         self.battery_capacity = battery_capacity
  30.  
  31.  
  32. my_car = Car()
  33. some_car1 = Car("Ford", "Mustang")
  34. some_car2 = Car(model="Camaro")
  35. some_car1.start_engine()
  36. some_car2.start_engine()
  37.  
  38. my_electric_car = ElectricCar(100, "Tesla", "Model 3")
  39. my_electric_car.start_engine()
  40.  
  41. my_electric_car2 = ElectricCar(60, "Toyota", "Prius")
  42. my_electric_car2.start_engine()
  43. my_electric_car2.stop_engine()
  44.  
Tags: checkio
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement