标签:小店 off open pos 电动 pen reading ret 管理
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 = str(self.year) + ‘ ‘ + self.make + ‘ ‘ + self.model return long_name.title() def read_odometer(self): print("This car has " + str(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!") def increment_odometer(self, miles): self.odometer_reading += miles class Battery(): """一次模拟电动汽车电瓶的简单尝试""" def __init__(self, battery_size=70): """初始化电瓶的属性""" self.battery_size = battery_size def describe_battery(self): """打印一条描述电瓶容量的消息""" print("This car has a " + str(self.battery_size) + "-kWh battery.") def get_range(self): """打印一条消息,指出电瓶的续航里程""" if self.battery_size == 70: range = 240 elif self.battery_size == 85: range = 270 message = "This car can go approximately " + str(range) message += " miles on a full charge." print(message) class ElectricCar(Car): """电动汽车的独特之处""" def __init__(self, make, model, year): # ? """初始化父类的属性""" super().__init__(make, model, year) # ? self.battery = Battery() my_tesla = ElectricCar(‘tesla‘, ‘model s‘, 2016) # ? print(my_tesla.get_descriptive_name()) my_tesla.battery.describe_battery() my_tesla.battery.get_range() """ 首先是Car 类的代码(见?)。创建子类时,父类必须包含在当前文件中,且位于子类前面。在?处,我们定义了子类ElectricCar 。定义子类时,必须在括号内指定父类的 名称。方法__init__() 接受创建Car 实例所需的信息(见?)。 ?处的super() 是一个特殊函数,帮助Python将父类和子类关联起来。这行代码让Python调用ElectricCar 的父类的方法__init__() ,让ElectricCar 实例包含父类的所 有属性。父类也称为超类 (superclass),名称super因此而得名。 为测试继承是否能够正确地发挥作用,我们尝试创建一辆电动汽车,但提供的信息与创建普通汽车时相同。在?处,我们创建ElectricCar 类的一个实例,并将其存储在变 量my_tesla 中。这行代码调用ElectricCar 类中定义的方法__init__() ,后者让Python调用父类Car 中定义的方法__init__() 。我们提供了实参‘tesla‘ 、‘model s‘ 和2016 。 """ """ 9.3.4 重写父类的方法 对于父类的方法,只要它不符合子类模拟的实物的行为,都可对其进行重写。为此,可在子类中定义一个这样的方法,即它与要重写的父类方法同名。这样,Python将不会考虑这 个父类方法,而只关注你在子类中定义的相应方法。 假设Car 类有一个名为fill_gas_tank() 的方法,它对全电动汽车来说毫无意义,因此你可能想重写它。下面演示了一种重写方式: def ElectricCar(Car): --snip-- def fill_gas_tank(): print("This car doesn‘t need a gas tank!") 现在,如果有人对电动汽车调用方法fill_gas_tank() ,Python将忽略Car 类中的方法fill_gas_tank() ,转而运行上述代码。使用继承时,可让子类保留从父类那里继 承而来的精华,并剔除不需要的糟粕。重点 """ """ 练习 9-6 冰淇淋小店 :冰淇淋小店是一种特殊的餐馆。编写一个名为IceCreamStand 的类,让它继承你为完成练习9-1或练习9-4而编写的Restaurant 类。这两个版 本的Restaurant 类都可以,挑选你更喜欢的那个即可。添加一个名为flavors 的属性,用于存储一个由各种口味的冰淇淋组成的列表。编写一个显示这些冰淇淋 的方法。创建一个IceCreamStand 实例,并调用这个方法。 9-7 管理员 :管理员是一种特殊的用户。编写一个名为Admin 的类,让它继承你为完成练习9-3或练习9-5而编写的User 类。添加一个名为privileges 的属性,用 于存储一个由字符串(如"can add post" 、"can delete post" 、"can ban user" 等)组成的列表。编写一个名为show_privileges() 的方法,它 显示管理员的权限。创建一个Admin 实例,并调用这个方法。 9-8 权限 :编写一个名为Privileges 的类,它只有一个属性——privileges ,其中存储了练习9-7 所说的字符串列表。将方法show_privileges() 移到这 个类中。在Admin 类中,将一个Privileges 实例用作其属性。创建一个Admin 实例,并使用方法show_privileges() 来显示其权限。 9-9 电瓶升级 :在本节最后一个electric_car.py版本中,给Battery 类添加一个名为upgrade_battery() 的方法。这个方法检查电瓶容量,如果它不是85,就将它 设置为85。创建一辆电瓶容量为默认值的电动汽车,调用方法get_range() ,然后对电瓶进行升级,并再次调用get_range() 。你会看到这辆汽车的续航里程增 加了。 """ class Restaurant(): """餐馆信息""" def __init__(self, name, type): """餐馆基本信息""" self.name = name self.type = type self.number_served = 0 def describe(self): """sasas""" print(self.name.title() + " is " + self.type.title() + " restaurant.") def open_restaurant(self): print(self.name.title() + " is openning.") # def number_served(self): # 此处函数名与参数名重合 def print_number_served(self): print(str(self.number_served) + " people have been this restaurant.") def set_number_served(self, number_served): self.number_served = number_served def increment_number_served(self, increment_served): self.number_served += increment_served class IceCreamStand(Restaurant): """冰淇淋小站""" def __init__(self, name, type): """初始化父类属性""" # super().__init__(self,name,type): # super().__init__(name, type) self.flavors = ["milk", "chocolate", "yogurt"] # 新增属性 相当于儿子比爹多的技能 # print("This icecreamstand offer " + self.flavors + ".")# 其他指令不应放在super()函数中 def print_icecream_menu(self): print("This icecreamstand offer:") for ice_cream in self.flavors: print("- " + ice_cream) # can only concatenate str (not "list") to str self.flavors需要调用str() matsuya = Restaurant("matsuya", "japanese") matsuya.describe() matsuya.print_number_served() matsuya.number_served = 200 matsuya.print_number_served() matsuya.set_number_served(100) matsuya.print_number_served() matsuya.increment_number_served(10) matsuya.print_number_served() mcdonald = IceCreamStand("mcdonald", "fastfood") mcdonald.describe() mcdonald.print_icecream_menu() class User(): """储存用户信息""" def __init__(self, first_name, last_name): """初始化信息""" self.first_name = first_name self.last_name = last_name self.login_attempts = 0 def describe(self): """pirnt uesrs‘s infos.""" print(self.first_name.title() + " " + self.last_name.title()) def greet_user(self): print( self.first_name.title() + " " + self.last_name.title() + " Welcome!") def increment_login_attempts(self): self.login_attempts += 1 def reset_login_attempts(self): self.login_attempts = 0 class Privileges(): # def __init__(self, privileges=["can add post", "can delete post", "can ban user"]): self.privileges = privileges def show_privileges(self): # self不要忘了 # privileges = self.privileges print("Your privileges are:\n" + str(self.privileges)) class Admin(User): def __init__(self, first_name, last_name): # super(first_name,last_name) # super() argument 1 must be type, not str super().__init__(first_name, last_name) # super(first_name,last_name) self.privileges = Privileges() phyllis = Admin("phyllis", "ryo") phyllis.describe() phyllis.privileges.show_privileges() # 调用时注意p不要大写‘Admin‘ object has no attribute ‘Privileges‘ 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 = str(self.year) + ‘ ‘ + self.make + ‘ ‘ + self.model return long_name.title() def read_odometer(self): print("This car has " + str(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!") def increment_odometer(self, miles): self.odometer_reading += miles class Battery(): """一次模拟电动汽车电瓶的简单尝试""" def __init__(self, battery_size=70): """初始化电瓶的属性""" self.battery_size = battery_size def upgrade_battery(self): # self.battery_size = battery_size # 多卵余 if self.battery_size != 85: self.battery_size = 85 def describe_battery(self): """打印一条描述电瓶容量的消息""" print("This car has a " + str(self.battery_size) + "-kWh battery.") def get_range(self): """打印一条消息,指出电瓶的续航里程""" if self.battery_size == 70: range = 240 elif self.battery_size == 85: range = 270 message = "This car can go approximately " + str(range) message += " miles on a full charge." print(message) class ElectricCar(Car): """电动汽车的独特之处""" def __init__(self, make, model, year): # ? """初始化父类的属性""" super().__init__(make, model, year) # ? self.battery = Battery() my_tesla = ElectricCar(‘tesla‘, ‘model s‘, 2016) # ? print(my_tesla.get_descriptive_name()) my_tesla.battery.describe_battery() my_tesla.battery.get_range() my_tesla.battery.upgrade_battery() # my_tesla.upgrade_battery() # 调用时没有打battery my_tesla.battery.get_range()
标签:小店 off open pos 电动 pen reading ret 管理
原文地址:https://www.cnblogs.com/phyllissRyo/p/10440987.html