/
vladimir_p
/
practic3_python
Обзор
Документация
Войти
/
vladimir_p
/
practic3_python
Код
Запросы
0
Задачи
Вики
Пакеты
0
Релизы
0
CI/CD
Аналитика
Безопасность
master
2.py
97 строк
3 KB
Vladimir P.
upload files
26 окт 2025, 21:47
26 окт 2025, 21:47
084c9a4
Код
Авторство
О чём код?
""" Demonstrates polymorphism through a vehicle class hierarchy. """ class Vehicle: """A base class to represent a vehicle.""" def __init__(self, brand: str, model: str, year: str) -> None: """ Initializes a Vehicle object. Args: brand: The brand of the vehicle. model: The model of the vehicle. year: The manufacturing year of the vehicle. """ self.brand = brand self.model = model self.year = year def __str__(self) -> str: """Returns the string representation of the vehicle.""" return f"{self.brand} {self.model} {self.year}" class Car(Vehicle): """Represents a car, inheriting from Vehicle.""" def __init__(self, brand: str, model: str, year: str, max_speed: int) -> None: """ Initializes a Car object. Args: brand: The brand of the car. model: The model of the car. year: The manufacturing year of the car. max_speed: The maximum speed in km/h. """ super().__init__(brand, model, year) self.max_speed = max_speed def __str__(self) -> str: """Returns the string representation of the car, including its max speed.""" return f"{self.brand} {self.model} {self.year}, max speed: {self.max_speed} km/h" class Bus(Vehicle): """Represents a bus, inheriting from Vehicle.""" def __init__(self, brand: str, model: str, year: str, seats: int) -> None: """ Initializes a Bus object. Args: brand: The brand of the bus. model: The model of the bus. year: The manufacturing year of the bus. seats: The number of available seats. """ super().__init__(brand, model, year) self.seats = seats def __str__(self) -> str: """Returns the string representation of the bus, including its seat count.""" return f"{self.brand} {self.model} {self.year}, total seats available: {self.seats}" class Bike(Vehicle): """Represents a bike, inheriting from Vehicle.""" def __init__(self, brand: str, model: str, year: str, acceleration: float) -> None: """ Initializes a Bike object. Args: brand: The brand of the bike. model: The model of the bike. year: The manufacturing year of the bike. acceleration: The acceleration in m/s^2. """ super().__init__(brand, model, year) self.acceleration = acceleration def __str__(self) -> str: """Returns the string representation of the bike, including its acceleration.""" return f"{self.brand} {self.model} {self.year}, acceleration: {self.acceleration} m/s^2" if __name__ == "__main__": car = Car("Toyota", "Camry", "2015", 250) bus = Bus("Volkswagen", "Transporter", "2000", 16) bike = Bike("Suzuki", "HAYABUSA", "2007", 9.26) print(car) print(bus) print(bike)