本文主要是介绍【Python编程-从入门到实践】练习题9-6 冰淇淋小店 9-7管理员 9-8权限 9-9电瓶升级,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
9-6 冰淇淋小店
class Restaurant():"""关于餐馆的一切信息"""def __init__(self, restaurant_name, cuisine_type):"""初始化餐厅信息"""self.restaurant_name = restaurant_nameself.cuisine_type = cuisine_typeself.number_served = 0def describe_restaurant(self):"""描述餐厅信息"""print("餐厅的名字叫:" + self.restaurant_name)def open_restaurant(self):"""指出餐厅是否开业"""print("餐厅正在营业。")def set_number_served(self, set_number_served):"""设置就餐人数"""self.number_served = set_number_servedprint("就餐人数为:" + str(self.number_served))def increment_number_served(self, increment_number_served):"""就餐人数递增"""self.number_served += increment_number_servedprint(self.number_served)class IceCreamStand(Restaurant):"""父类Restaurant的子类"""def __init__(self, restaurant_name, cuisine_type):"""初始化子类的方法"""super().__init__(restaurant_name, cuisine_type)self.flavors = [] #创建一个用于存储列表的属性def show_ice(self, *ice_s): #设一个列表形参"""显示冰淇淋"""self.flavors = ice_s #把形参赋值给属性print("冰淇淋有:")for i in self.flavors: #打印列表print("- " + i.title())ice_cream_stand = IceCreamStand('人间美味', '中餐厅') #创建实例ice_cream_stand.show_ice('冰1','冰2','冰3') #传递任意数量的参数'''
必须创建实例,才能调用子类的方法。就是说白了,你如果没有创建一个名为‘人间美味’的
‘中餐厅’,你就没办法去调用子类的方法,所以很多人在创建实例ice_cream_stand,忘记
传参创建实例,导致报错(缺少两个参数)
'''
运行结果:
9-7 管理员
class User():"""关于用户的一切"""def __init__(self, first_name, last_name):"""初始化用户信息"""self.fi_name = first_nameself.la_name = last_namelogin_attempts = 0def describe_user(self):"""打印用户信息"""print(self.fi + self.la)def greet_user(self):"""向用户问好"""print("你好," + self.fi + self.la)def increment_login_attempts(self):"""试图登录的用户"""login_attempts = 1print("现在正在试图登录的用户有:" + str(login_attempts) + "个.")def reset_login_attempts(self):"""试图登录的用户数"""login_attempts = 0print("现在正在试图登录的用户有:" + str(login_attempts) + "个.")class Admin(User):"""继承User"""def __init__(self, first_name, last_name):"""初始化子类的属性"""super().__init__(first_name, last_name)self.privileges = ['can add post', 'can delete post', 'can ban user']def show_privileges(self):"""显示超级管理员的权限"""print("超级管理员的权限有:")for admin_privilege in self.privileges:print("- " + admin_privilege)a_admin = Admin('guai', 'guai')a_admin.show_privileges()
运行结果:
9-8 权限
class User():"""关于用户的一切"""def __init__(self, first_name, last_name):"""初始化用户信息"""self.fi_name = first_nameself.la_name = last_namelogin_attempts = 0def describe_user(self):"""打印用户信息"""print(self.fi + self.la)def greet_user(self):"""向用户问好"""print("你好," + self.fi + self.la)def increment_login_attempts(self):"""试图登录的用户"""login_attempts = 1print("现在正在试图登录的用户有:" + str(login_attempts) + "个.")def reset_login_attempts(self):"""试图登录的用户数"""login_attempts = 0print("现在正在试图登录的用户有:" + str(login_attempts) + "个.")class Privileges():"""管理权限的类"""def __init__(self, privileges=[]):"""初始化属性"""self.privileges = ['can add post', 'can delete post', 'can ban user']def show_privileges(self):"""显示超级管理员的权限"""print("超级管理员的权限有:")for admin_privilege in self.privileges:print("- " + admin_privilege) class Admin(User):"""继承User"""def __init__(self, first_name, last_name):"""初始化子类的属性"""super().__init__(first_name, last_name)#在调用Privileges时,如果需要参数,就应该给参数self.privileges = Privileges()a_admin_2 = Admin('xiao', 'guai')a_admin_2.privileges.show_privileges()
注意点(看如下两图):
9-9 电瓶升级
class Car():"""关于汽车的一切信息"""def __init__(self, make, model, year):"""初始化汽车信息"""self.make = makeself.model = modelself.year = yearself.odometer_reading = 0def get_descriptive_name(self):"""描述用户信息"""long_name = str(self.year) + ' ' + self.make + ' ' + self.modelreturn long_name.title()def read_odometer(self):"""读取公里数"""print("This car has " + str(self.ofometer_reading) +"miles on it.")def update_odometer(self, mileage):"""增加公里数"""if mileage >= self.odometer_reading:self.odometer_reading = mileageelse:print("You can't roll back an odometer!")def incremet_odometer(self, miles):"""增长的公里数"""self.odometer_reading += miles#将实例用作属性:将类中的一部分独立出来成为一个小类,方便管理使用
class Battery():"""一次模拟电动汽车电瓶的简单尝试"""def __init__(self, battery_size=70):"""初始化电瓶的属性"""self.battery_size = battery_sizedef describe_battery(self):"""打印一条描述电瓶容量的消息"""print("This car has a " + str(self.battery_size) + "-kwh battery")def get_range(self):"""打印一条消息,指出电瓶的续航里程"""if self.battery_size == 70:range = 240elif self.battery_size == 85:range = 270message = "This car can go approximately " + str(range)message += " miles on a full charge."print(message)def upgrade_battery(self):"""检查电瓶容量"""if self.battery_size != 85:self.battery_size = 85class ElectricCar(Car):"""特殊的电动汽车"""def __init__(self, make, model, year):"""初始化电动汽车属性电动汽车自己特殊的方法和属性"""super().__init__(make, model, year) #继承self.battery_a = Battery() #将实例用作属性def describe_battery(self):"""打印一条描述电瓶容量的消息"""print("This car has a " + str(self.battery_size) + "-Kwh battery.")def fill_gas_tank(self): #重写父类的方法"""电动汽车没有汽油"""print("This car doesn't need a gas tank!") a_new_tesla = ElectricCar('tesla', 'model s', 2021)a_new_tesla.battery_a.describe_battery() a_new_tesla.battery_a.get_range() #调用方法查看续航里程a_new_tesla.battery_a.upgrade_battery() #调用方法升级了电瓶容量a_new_tesla.battery_a.get_range() #再次调用方法查看续航里程
运行结果:
这篇关于【Python编程-从入门到实践】练习题9-6 冰淇淋小店 9-7管理员 9-8权限 9-9电瓶升级的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!