这个问题对于我说有点难,我运行好几次都不行,求解。

编写一个汽车car类,类中用brand和country存储了汽车类的品牌名称和生产国家,类中有一个show_brand()函数可以打印出这个汽车的品牌名称。再编写一个电动车E_car类,这个类继承汽车car类,且有自己的battery变量存储了电池的续航里程,且有自己的show_battery()方法可以打印自己的续航里程数。之后,创建一个car类的对象,再创建一个E_car类的对象。

可以参考一下下面代码


class car:
    def __init__(self, brand, country):
        self.brand = brand
        self.country = country
    def show_brand(self):
        print(f'汽车品牌:{self.brand}')


class E_car(car):
    def __init__(self, brand, country, battery):
        super().__init__(brand, country)
        self.battery = battery
    def show_battery(self):
        print(f'汽车续航:{self.battery}')


c = car('aa', 'ch')
c.show_brand()
ec = E_car('bb', 'ch', 450)
ec.show_brand()
ec.show_battery()

img