Python 面向对象程序设计实验报告 - 汽车类、继承和多态
Python 面向对象程序设计实验报告
实验名称:Python 面向对象程序设计
实验目的:
- 掌握 Python 面向对象编程的基本概念和方法;
- 学习如何定义类、创建对象和调用对象方法;
- 理解继承、多态等面向对象编程的高级概念。
实验环境:
- Python 3.0 及以上版本;
- PyCharm 集成开发环境。
实验内容:
- 定义一个汽车类,包含汽车品牌、型号、颜色、价格等属性,以及加速、刹车、转向等方法;
- 定义一个小轿车类,继承汽车类,增加小轿车特有的属性和方法,如载客量、油耗、自动驾驶等;
- 定义一个卡车类,继承汽车类,增加卡车特有的属性和方法,如载重量、货箱长度、车速限制等;
- 利用多态性,编写一个函数,对不同类型的汽车进行加速、刹车和转向等操作;
- 编写测试代码,创建不同类型的汽车对象,测试各种方法是否能够正确执行。
实验步骤:
- 定义汽车类 Car,包含品牌'brand'、型号'model'、颜色'color'、价格'price' 和速度'speed' 等属性,以及加速'accelerate'、刹车'brake'、转向'turn' 等方法。
class Car:
def __init__(self, brand, model, color, price, speed):
self.brand = brand
self.model = model
self.color = color
self.price = price
self.speed = speed
def accelerate(self):
self.speed += 10
print('加速后的速度为:', self.speed)
def brake(self):
self.speed -= 10
print('刹车后的速度为:', self.speed)
def turn(self, direction):
print('向', direction, '转向')
- 定义小轿车类 CompactCar,继承汽车类 Car,增加载客量'passenger'、油耗'fuel'、自动驾驶'autopilot' 等属性和方法。
class CompactCar(Car):
def __init__(self, brand, model, color, price, speed, passenger, fuel, autopilot):
super().__init__(brand, model, color, price, speed)
self.passenger = passenger
self.fuel = fuel
self.autopilot = autopilot
def __str__(self):
return 'CompactCar: ' + self.brand + ' ' + self.model
def get_passenger(self):
print('该车可载客人数为:', self.passenger)
def get_fuel(self):
print('该车油耗为:', self.fuel)
def get_autopilot(self):
if self.autopilot:
print('该车支持自动驾驶')
else:
print('该车不支持自动驾驶')
- 定义卡车类 Truck,继承汽车类 Car,增加载重量'load'、货箱长度'length'、车速限制'speed_limit' 等属性和方法。
class Truck(Car):
def __init__(self, brand, model, color, price, speed, load, length, speed_limit):
super().__init__(brand, model, color, price, speed)
self.load = load
self.length = length
self.speed_limit = speed_limit
def __str__(self):
return 'Truck: ' + self.brand + ' ' + self.model
def get_load(self):
print('该车最大载重量为:', self.load)
def get_length(self):
print('该车货箱长度为:', self.length)
def get_speed_limit(self):
print('该车限速为:', self.speed_limit)
- 利用多态性,编写一个操作汽车的函数,对不同类型的汽车进行加速、刹车和转向等操作。
def operate_car(car):
car.accelerate()
car.brake()
car.turn('左')
- 编写测试代码,创建不同类型的汽车对象,测试各种方法是否能够正确执行。
if __name__ == '__main__':
car1 = Car('BMW', 'X6', '黑色', 1200000, 80)
print(car1.brand, car1.model, car1.color, car1.price, car1.speed)
car1.accelerate()
car1.brake()
car1.turn('右')
print()
car2 = CompactCar('Tesla', 'Model S', '白色', 800000, 100, 5, '电动', True)
print(car2.brand, car2.model, car2.color, car2.price, car2.speed)
car2.get_passenger()
car2.get_fuel()
car2.get_autopilot()
operate_car(car2)
print()
car3 = Truck('Volvo', 'FH16', '红色', 2000000, 60, 50, 15, 80)
print(car3.brand, car3.model, car3.color, car3.price, car3.speed)
car3.get_load()
car3.get_length()
car3.get_speed_limit()
operate_car(car3)
实验结果:
运行测试代码,输出结果如下:
BMW X6 黑色 1200000 80
加速后的速度为: 90
刹车后的速度为: 80
向 右 转向
Tesla Model S 白色 800000 100
该车可载客人数为: 5
该车油耗为: 电动
该车支持自动驾驶
加速后的速度为: 110
刹车后的速度为: 100
向 左 转向
Volvo FH16 红色 2000000 60
该车最大载重量为: 50
该车货箱长度为: 15
该车限速为: 80
加速后的速度为: 70
刹车后的速度为: 60
向 左 转向
实验结论:
本次实验中,我们通过定义汽车类、小轿车类和卡车类,学习了 Python 面向对象编程的基本概念和方法。在实现三个类的时候,我们利用了继承的特性,把汽车类的属性和方法传递给了子类,并在子类中增加了特定的属性和方法。由于小轿车类和卡车类都是汽车类的子类,因此我们可以利用多态性,编写一个操作汽车的函数,对不同类型的汽车进行加速、刹车和转向等操作。最后,我们编写了测试代码,创建不同类型的汽车对象,测试各种方法是否能够正确执行。实验结果表明,我们的程序运行良好,能够正确地输出结果。
原文地址: https://www.cveoy.top/t/topic/oxYv 著作权归作者所有。请勿转载和采集!