# 动手试一试
# 9-1 餐厅
class Restaurant():
def __init__(self, restaurant_name, cuisine_type):
"""初始化属性restaurant_name和cuisine_type"""
self.restaurant_name = restaurant_name
self.cuisine_type = cuisine_type
def describe_restaurant(self):
"""餐厅信息"""
print(self.restaurant_name.title() + " is opening.")
print(self.restaurant_name.title() + "'s food is " + self.cuisine_type)
def open_restaurant(self):
"""餐厅营业时间"""
print(self.restaurant_name + " is opening at 10:00~22:00.")
good = Restaurant('tilly', 'chinese')
print(good.restaurant_name.title())
print(good.cuisine_type.title())
good.describe_restaurant()
good.open_restaurant()
# 9-2 三家餐厅:重新创建三个实例
print("\n")
happy = Restaurant('hili', 'france')
print(happy.restaurant_name.title())
print(happy.cuisine_type.title())
happy.describe_restaurant()
happy.open_restaurant()
print("\n")
nice = Restaurant('cidy', 'american')
print(nice.restaurant_name.title())
print(nice.cuisine_type.title())
nice.describe_restaurant()
nice.open_restaurant()
# 9-3 用户:有问题
class User():
def __init__(self, first_name, last_name, tel, address):
self.first_name = first_name
self.last_name = last_name
self.tel = tel
self.address = address
def describe_user(self):
print(self.first_name + self.last_name + "的电话号码是:" + self.tel)
print("他(她)的住址是:" + self.address)
def greet_user(self):
print(self.first_name + self.last_name + "您好,欢迎您来到安徽合肥!")
wang = User('王','三','124567899','上海')
wang.describe_user()
wang.greet_user()
print("\n")
zhang = User('张','五','4343555555','北京')
zhang.describe_user()
zhang.greet_user()
print("\n")
huang = User('黄','八','12308','香港')
huang.describe_user()
huang.greet_user()
# 9-4 就餐人数
class Restaurant():
def __init__(self, restaurant_name, cuisine_type):
"""初始化属性restaurant_name和cuisine_type"""
self.restaurant_name = restaurant_name
self.cuisine_type = cuisine_type
self.number_served = 0
def person(self):
"""定义这家餐厅有多少人吃饭"""
print(self.restaurant_name.title() + "餐厅有" + str(self.number_served) + "人在吃饭。")
def set_number_served(self,number1):
self.number_served = number1
def increment_number_served(self,number2):
self.number_served += number2
restaurant = Restaurant('tilly', 'chinese')
restaurant.person()
print("\n")
restaurant.number_served = 50
restaurant.person()
print("\n")
restaurant.set_number_served(20)
restaurant.person()
print("\n")
restaurant.set_number_served(100)
restaurant.increment_number_served(80)
restaurant.person()
# 9-5 尝试登录次数:
class User():
def __init__(self, first_name, last_name, tel, address):
self.first_name = first_name
self.last_name = last_name
self.tel = tel
self.address = address
self.login_attempts = 0
def login_number(self):
print(self.first_name + self.last_name + "总共注册了" + str(self.login_attempts) + "次。" )
def increment_login_attempts(self,add_number):
self.login_attempts += add_number
def reset_login_attempts(self,reset_number):
self.login_attempts = reset_number
zhang = User('张','三','23333333','香港')
zhang.increment_login_attempts(1)
zhang.increment_login_attempts(1)
zhang.increment_login_attempts(1)
zhang.increment_login_attempts(1)
zhang.increment_login_attempts(1)
zhang.increment_login_attempts(1)
zhang.login_number()
print("\n")
zhang.reset_login_attempts(0)
zhang.login_number()
# 9-6 冰淇淋小店
class Restaurant():
def __init__(self, restaurant_name, cuisine_type):
"""初始化属性restaurant_name和cuisine_type"""
self.restaurant_name = restaurant_name
self.cuisine_type = cuisine_type
def describe_restaurant(self):
"""餐厅信息"""
print(self.restaurant_name.title() + " is opening.")
print(self.restaurant_name.title() + "'s food is " + self.cuisine_type)
def open_restaurant(self):
"""餐厅营业时间"""
print(self.restaurant_name + " is opening at 10:00~22:00.")
class IneCreamStand(Restaurant):
"""继承9-4,冰淇淋特殊餐厅"""
def __init__(self, restaruant_name, cuisine_type):
super().__init__(restaruant_name, cuisine_type)
self.flavors = ['vanilla', 'chocolate', 'durian']
def print_flavors(self):
print("\n")
for flavor in self.flavors:
print(flavor)
restaurant_4 = IneCreamStand('麦当劳甜品站', '冰淇淋')
restaurant_4.print_flavors()
# 9-7 管理员:编写一个名为Admin的类,让他继承练习9-3的User类
class User():
def __init__(self, first_name, last_name, tel, address):
self.first_name = first_name
self.last_name = last_name
self.tel = tel
self.address = address
self.login_attempts = 0
def login_number(self):
print(self.first_name + self.last_name + "总共注册了" + str(self.login_attempts) + "次。" )
def increment_login_attempts(self,add_number):
self.login_attempts += add_number
def reset_login_attempts(self,reset_number):
self.login_attempts = reset_number
class Admin(User):
"""特殊的用户,管理员"""
def __init__(self, privileges):
self.privileges = privileges
def show_privileges(self):
for i in self.privileges:
print(i)
my_Admin = Admin(["can add post", "can delete post", "cna ban user"])
my_Admin.show_privileges()
# 9-8 权限
class Privileges():
def __init__(self, privileges):
self.privileges = privileges
def show_privileges(self):
for i in self.privileges:
print(i)
class Admin(Privileges):
def __init__(self, privileges):
super().__init__(privileges)
my_Admin = Admin(["can add post", "can delete post", "cna ban user"])
my_Admin.show_privileges()
# 9-9 电瓶升级
class Battery():
def __init__(self, battery_size = 70):
self.battery_size = battery_size
def describe_battery(self):
print(f"这辆车有{self.battery_size}千米的电池。")
def upgrade_battery(self):
if self.battery_size == 85:
self.battery_size = 85
else:
self.battery_size = 85
def get_range(self):
if self.battery_size == 70:
range = 240
elif self.battery_size == 85:
range = 270
message = "这辆车可以跑" + str(range)
message += "千米的路程"
print(message)
my_tesla = Battery()
my_tesla.upgrade_battery()
my_tesla.get_range()
# 9-10 导入Restaurant类
# restaurant.py
class Restaurant():
def __init__(self, restaurant_name, cuisine_type):
self.restaurant_name = restaurant_name
self.cuisine_type = cuisine_type
def describe_restaurant(self):
print(self.restaurant_name.title() + " is opening.")
print(self.restaurant_name.title() + "'s food is " + self.cuisine_type)
def open_restaurant(self):
print(self.restaurant_name + " is opening at 10:00~22:00.")
class IneCreamStand(Restaurant):
def __init__(self, restaruant_name, cuisine_type):
super().__init__(restaruant_name, cuisine_type)
self.flavors = ['vanilla', 'chocolate', 'durian']
def print_flavors(self):
print("\n")
for flavor in self.flavors:
print(flavor)
from restaurant import Restaurant,IneCreamStand
Restaurant = IneCreamStand('麦当劳甜品站', '冰淇淋')
Restaurant.print_flavors()
# 9-11 导入Admin类
# admin.py
class User():
def __init__(self, first_name, last_name, tel, address):
self.first_name = first_name
self.last_name = last_name
self.tel = tel
self.address = address
self.login_attempts = 0
def login_number(self):
print(self.first_name + self.last_name + "总共注册了" + str(self.login_attempts) + "次。" )
def increment_login_attempts(self,add_number):
self.login_attempts += add_number
def reset_login_attempts(self,reset_number):
self.login_attempts = reset_number
class Admin(User):
def __init__(self, privileges):
self.privileges = privileges
def show_privileges(self):
for i in self.privileges:
print(i)
class Privileges():
def __init__(self, privileges):
self.privileges = privileges
def show_privileges(self):
for i in self.privileges:
print(i)
class Admin(Privileges):
def __init__(self, privileges):
super().__init__(privileges)
from admin import User,Admin,Privileges
my_Admin = Admin(["can add post", "can delete post", "cna ban user"])
my_Admin.show_privileges()
# 9-12 多个模块
# user.py
class User():
def __init__(self, first_name, last_name, tel, address):
self.first_name = first_name
self.last_name = last_name
self.tel = tel
self.address = address
self.login_attempts = 0
def login_number(self):
print(self.first_name + self.last_name + "总共注册了" + str(self.login_attempts) + "次。" )
def increment_login_attempts(self,add_number):
self.login_attempts += add_number
def reset_login_attempts(self,reset_number):
self.login_attempts = reset_number
# privilegesadmin.py
from user import User
class Admin(User):
def __init__(self, privileges):
self.privileges = privileges
def show_privileges(self):
for i in self.privileges:
print(i)
class Privileges():
def __init__(self, privileges):
self.privileges = privileges
def show_privileges(self):
for i in self.privileges:
print(i)
class Admin(Privileges):
def __init__(self, privileges):
super().__init__(privileges)
from privilegesadmin import Privileges,Admin
my_Admin = Admin(["can add post", "can delete post", "cna ban user"])
my_Admin.show_privileges()
# 9-13 使用OrderedDict
from collections import OrderedDict
dics = OrderedDict()
dics['int'] = '整形'
dics['bool'] = '布尔类型'
dics['float'] = '浮点型'
dics['str'] = '字符串'
dics['list'] = '列表'
for k,v in dics.items():
print(k + ':' + v)
# 9-14 骰子
from random import randint
x = randint(1, 6)
print(x)
from random import randint
class Die():
def __init__(self):
self.sides = 6
def roll_die6(self):
x = randint(1, 6)
print(x)
def roll_die10(self):
x = randint(1, 10)
print(x)
def roll_die20(self):
x = randint(1, 20)
print(x)
my_die = Die()
print("------------6sides---------------")
for i in range(10):
my_die.roll_die6()
print("------------10sides---------------")
for i in range(10):
my_die.roll_die10()
print("------------20sides---------------")
for i in range(10):
my_die.roll_die20()
# 9-15 Python Module of the Week
《Python编程:从入门到实践》习题答案——第9章 类
最新推荐文章于 2024-08-14 12:00:04 发布