定义类Car,具有隐藏属性car_name和car_color , 定义类Benz,继承自Car,为 Benz添加新的隐藏属性car_vol, 为Benz定义set和aet方法’T功能目LTA添I限概属性 car_vol,0希香 、 ar_Vol的值。63024 实例化Benz类以创建对象,调用对象的方法,打印对象的car_name、car_color、car_vol相应的值。
时间: 2024-03-01 13:51:01 浏览: 111
好的,以下是代码实现:
```python
class Car:
def __init__(self, name, color):
self.__car_name = name
self.__car_color = color
def get_car_name(self):
return self.__car_name
def get_car_color(self):
return self.__car_color
class Benz(Car):
def __init__(self, name, color, vol):
super().__init__(name, color)
self.__car_vol = vol
def get_car_vol(self):
return self.__car_vol
def set_car_vol(self, vol):
self.__car_vol = vol
# 实例化Benz类
my_benz = Benz("Benz S-Class", "Black", 4)
# 打印对象的属性值
print("Car name:", my_benz.get_car_name())
print("Car color:", my_benz.get_car_color())
print("Car vol:", my_benz.get_car_vol())
# 修改car_vol的值
my_benz.set_car_vol(5)
# 再次打印对象的属性值
print("Car name:", my_benz.get_car_name())
print("Car color:", my_benz.get_car_color())
print("Car vol:", my_benz.get_car_vol())
```
输出结果:
```
Car name: Benz S-Class
Car color: Black
Car vol: 4
Car name: Benz S-Class
Car color: Black
Car vol: 5
```
阅读全文