|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
class Car:
def __init__(self,make,model,year):
self.make = make
self.model= model
self.year =year
self.odometer_reading = 0
def get_descriptive_name(self):
long_name = f"{self.year} {self.make} {self.model}"
return long_name.title()
def read_odometer(self):
print(f"This car has {self.odometer_reading} miles on it.")
def update_odometer(self,mileage):
if mileage >= self.odometer_reading:
self.odometer_reading = mileage
else:
print("You can't roll back an odometer!")
my_new_car = Car('audi','a4','2019')
print(my_new_car.get_descriptive_name())
my_new_car.update_odometer(23)
my_new_car.read_odometer()
class Battery:
def __init__(self,battery_size = 75):
self.battery_size = battery_size
def describe_battery(self):
print(f"This car has a {self.battery_size}-kwh battery.")
class ElectricCar(Car):
def __init__(self,make,model,year):
super().__init__(make,model,year)
self.battery = Battery()
my_tesla = ElectricCar('tesla','models','2019')
print(my_tesla.get_descriptive_name())
my_tesla.battery.describe_battery()
代码运行正常 子类跟父类一起打印出来的
我想问怎么只要子类 父类不要打出来 |
|