昨日知识点回顾:
Dog类属性的调用和Car类属性值的修改
今日知识点学习:
9.3 继承
9.3.1 子类的方法__init__()
class Car:
"""一次模拟汽车的简单尝试"""
def __init__(self, make, model, year):
"""初始化属性"""
self.make = make
self.model = model
self.year = year
self.odo_reading = 0
def get_ifo(self):
"""返回汽车完整信息"""
long_name = f"{self.year} {self.make} {self.model}"
return long_name.title()
class ElectricCar(Car):#一定要写入父类
"""电动汽车的独特之处"""
def __init__(self, make, model, year):
"""初始化父类的属性"""
super().__init__(make, model, year)
my_tesla = ElectricCar('tesla', 'model S', '2017')
print(my_tesla.get_ifo())
# 运行结果:
# 2017 Tesla Model S
9.3.2 给子类定义属性和方法
class Car:
"""一次模拟汽车的简单尝试"""
def __init__(self, make, model, year):
"""初始化属性"""
self.make = make
self.model = model
self.year = year
self.odo_reading = 0
def get_ifo(self):
"""返回汽车完整信息"""
long_name = f"{self.year} {self.make} {self.model}"
return long_name.title()
def read_odo(self):
"""打印汽车里程数"""
print(f"The car has {self.odo_reading} miles on it.")
# int类型可以被调用与字符串一起输出
# odo = str(self.odo_reading)
# print(f"The car has {odo} miles on it.")
def update_odo(self, miles):
"""更新公里数,禁止往回调"""
if miles >= self.odo_reading:
self.odo_reading = miles
else:
print('你不能回调公里数')
def increment_odo(self, miles):
"""将里程表增加指定公里数"""
self.odo_reading += miles
class ElCar(Car):
"""电动汽车的独特之处"""
def __init__(self, make, model, year):
"""初始化父类的属性"""
# super().__init__(make,model,year)
super(ElCar, self).__init__(make, model, year)
self.battery_size = 75
def battery_ifo(self):
print(f"The car's battery is {self.battery_size}-kWh.")
my_tesla = ElCar('tesla', 'model S', '2017')
print(my_tesla.get_ifo())
my_tesla.battery_ifo()
# 运行结果:
# 2017 Tesla Model S
# The car's battery is 75-kWh.
9.3.3 重写父类的方法
class Car:
"""一次模拟汽车的简单尝试"""
def __init__(self, make, model, year):
"""初始化属性"""
self.make = make
self.model = model
self.year = year
self.odo_reading = 0
def get_ifo(self):
"""返回汽车完整信息"""
long_name = f"{self.year} {self.make} {self.model}"
return long_name.title()
def read_odo(self):
"""打印汽车里程数"""
print(f"The car has {self.odo_reading} miles on it.")
# int类型可以被调用与字符串一起输出
# odo = str(self.odo_reading)
# print(f"The car has {odo} miles on it.")
def update_odo(self, miles):
"""更新公里数,禁止往回调"""
if miles >= self.odo_reading:
self.odo_reading = miles
else:
print('你不能回调公里数')
def increment_odo(self, miles):
"""将里程表增加指定公里数"""
self.odo_reading += miles
def fill_gas_tank(self):
"""打印一条加满油的信息"""
print('油加满')
class ElCar(Car):
"""电动汽车的独特之处"""
def __init__(self, make, model, year):
"""初始化父类的属性"""
# super().__init__(make,model,year)
super(ElCar, self).__init__(make, model, year)
self.battery_size = 75
def battery_ifo(self):
"""打印一条电动车电池的信息"""
print(f"The car's battery is {self.battery_size}-kWh.")
def fill_gas_tank(self):
"""打印一条电动车不需要加油的信息"""
print('不需要加油')
my_tesla = ElCar('tesla', 'model S', '2017')
print(my_tesla.get_ifo())
my_tesla.battery_ifo()
my_tesla.fill_gas_tank()
print('\nYour car fill gas tank:')
your_car = Car('audi', 'a4', 2019)
your_car.fill_gas_tank()
# 运行结果:
# 2017 Tesla Model S
# The car's battery is 75-kWh.
# 不需要加油
#
# Your car fill gas tank:
# 油加满
9.3.4 将实例用作属性
class Car:
"""一次模拟汽车的简单尝试"""
def __init__(self, make, model, year):
"""初始化属性"""
self.make = make
self.model = model
self.year = year
self.odo_reading = 0
def get_ifo(self):
"""返回汽车完整信息"""
long_name = f"{self.year} {self.make} {self.model}"
return long_name.title()
def read_odo(self):
"""打印汽车里程数"""
print(f"The car has {self.odo_reading} miles on it.")
def update_odo(self, miles):
"""更新公里数,禁止往回调"""
if miles >= self.odo_reading:
self.odo_reading = miles
else:
print('你不能回调公里数')
def increment_odo(self, miles):
"""将里程表增加指定公里数"""
self.odo_reading += miles
def fill_gas_tank(self):
"""打印一条加满油的信息"""
print('油加满')
class ElCar(Car):
"""电动汽车的独特之处"""
def __init__(self, make, model, year):
"""初始化父类的属性"""
# super().__init__(make,model,year)
super(ElCar, self).__init__(make, model, year)
self.battery = Battery()
def fill_gas_tank(self):
"""打印一条电动车不需要加油的信息"""
print('不需要加油')
class Battery:
"""一次模拟电动汽车电瓶的简单尝试"""
def __init__(self, battery_size=75):
"""初始化电瓶的值"""
self.battery_size = battery_size
def describe_battery(self):
"""打印一条电瓶容量的信息"""
print(f"The car's battery is {self.battery_size}-kWh.")
def get_range(self):
"""打印电瓶的续航里程"""
rang = 0
if self.battery_size == 75:
rang = 260
if self.battery_size == 100:
rang = 315
print(f"This car go about {rang} miles on a full charge.")
my_tesla = ElCar('tesla', 'model S', '2017')
print(my_tesla.get_ifo())
my_tesla.fill_gas_tank()
my_tesla.battery.describe_battery()
my_tesla.battery.get_range()
# 运行结果:
# 2017 Tesla Model S
# 不需要加油
# The car's battery is 75-kWh.
# This car go about 260 miles on a full charge.
9.3.5 模拟实物
用代码来表示实物