1、对car类增加颜色(color)属性,并参考51.2节对颜色(color)属性进行数据封装。2、采用多重勇的方式,在现有类的基础上定义一个能像飞机一样飞行的小汽车类( aircraf-car )
时间: 2023-12-10 13:38:05 浏览: 32
1. 对Car类增加颜色(color)属性并进行数据封装的代码如下:
```python
class Car:
def __init__(self, make, model, year, color):
self.__make = make
self.__model = model
self.__year = year
self.__color = color
def get_make(self):
return self.__make
def get_model(self):
return self.__model
def get_year(self):
return self.__year
def get_color(self):
return self.__color
def set_color(self, color):
self.__color = color
```
2. 定义一个能像飞机一样飞行的小汽车类(aircraft_car)的代码如下:
```python
class AircraftCar(Car):
def __init__(self, make, model, year, color):
super().__init__(make, model, year, color)
self.__altitude = 0
def get_altitude(self):
return self.__altitude
def set_altitude(self, altitude):
self.__altitude = altitude
def fly(self, altitude):
self.set_altitude(altitude)
print(f"The {self.get_color()} {self.get_make()} {self.get_model()} is flying at an altitude of {self.get_altitude()} feet.")
```
这个小汽车类继承自Car类,并添加了一个高度属性。它还有一个名为fly的方法,可以将小汽车飞起来,并打印出它的高度。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![dmg](https://img-home.csdnimg.cn/images/20210720083646.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)