Python学习日志 - 9.类(习题解答)

#9-1 餐馆
class Restaurant():
def __init__(name, ctype):
self.Restaurant_name = name
self.Cuisine_type = ctype

def describe_restaurant():
print("Name: " + str(self.Restaurant_name))
print("Type: " + str(self.Cuisine_type))

def open_reastaurant():
print("The Restaurant is opened")

my_restaurant = Restaurant("New", "Unknown")
my_restaurant.describe_restaurant()
my_restaurant.open_reastaurant()

#9-2 三家餐馆
class Restaurant():
def __init__(name, ctype):
self.Restaurant_name = name
self.Cuisine_type = ctype

def describe_restaurant():
print("Name: " + str(self.Restaurant_name))
print("Type: " + str(self.Cuisine_type))

def open_reastaurant():
print("The Restaurant is opened")

my_restaurant1 = Restaurant("r1", "Unknown")
my_restaurant1.describe_restaurant()
my_restaurant2 = Restaurant("r2", "Unknown")
my_restaurant2.describe_restaurant()
my_restaurant3 = Restaurant("r3", "Unknown")
my_restaurant3.describe_restaurant()

#9-3 用户
class User():
def __init__(first_name, last_name):
self.first_name = first_name
self.last_name = last_name
self.profile = {}

def describe_user():
print("First name: " + self.first_name)
print("Last name: " + self.last_name)
print("User infomation: " str(self.profile))

def greet_user():
print("Hello, " + self.first_name + " " + self.last_name)

user1 = User("Bill", "Gates")
user2 = User("David", "Mao")
user1.describe_user()
user1.greet_user()
user2.describe_user()
user2.greet_user()

#9-4 就餐人数
class Restaurant():
def __init__(name, ctype, num = 0):
self.Restaurant_name = name
self.Cuisine_type = ctype
self.number_served = num

def describe_restaurant():
print("Name: " + str(self.Restaurant_name))
print("Type: " + str(self.Cuisine_type))

def open_reastaurant():
print("The Restaurant is opened")

my_restaurant = Restaurant("New", "Unknown")
print(my_restaurant.number_served)
my_restaurant.number_served = 1
print(my_restaurant.number_served)
my_restaurant.describe_restaurant()
my_restaurant.open_reastaurant()

#9-5 尝试登陆次数
class User():
def __init__(first_name, last_name):
self.first_name = first_name
self.last_name = last_name
self.profile = {}
self.login_attempts = 0

def describe_user():
print("First name: " + self.first_name)
print("Last name: " + self.last_name)
print("User infomation: " + str(self.profile))

def greet_user():
print("Hello, " + self.first_name + " " + self.last_name)

def increment_login_attempts():
self.login_attempts += 1

def reset_login_attempts():
self.login_attempts = 0

my_user = User("Bill", "Gates")
print(my_user.login_attempts)
my_user.increment_login_attempts()
print(my_user.login_attempts)
my_user.reset_login_attempts()
print(my_user.login_attempts)

#9-6 冰淇淋小店
class Restaurant():
def __init__(name, ctype):
self.Restaurant_name = name
self.Cuisine_type = ctype

def describe_restaurant():
print("Name: " + str(self.Restaurant_name))
print("Type: " + str(self.Cuisine_type))

def open_reastaurant():
print("The Restaurant is opened")

def IceCreamStand(Restaurant):
def __init__(name, ctype):
super().__init__(name, ctype)
self.flavors = ['Milk', "Coco", "Melon"]
def get_flavors():
return self.flavors

my_icecreamstand = IceCreamStand("i1", "Ice")
print(my_icecreamstand.get_flavors())

#9-7 管理员
class User():
def __init__(first_name, last_name):
self.first_name = first_name
self.last_name = last_name
self.profile = {}

def describe_user():
print("First name: " + self.first_name)
print("Last name: " + self.last_name)
print("User infomation: " str(self.profile))

def greet_user():
print("Hello, " + self.first_name + " " + self.last_name)

class Admin(User):
def __init__(first_name, last_name):
super().__init__(first_name, last_name)
self.privileges = ["can add post", "can delete post", "can ban user"]
def show_privileges():
print(self.privileges)

my_admin = Admin("Bill","Gates")
my_admin.show_privileges()

#9-10 导入Restaurant类
from ch9 import Restaurant
my_restaurant = Restaurant("New", "Unknown")
my_restaurant.describe_restaurant()
my_restaurant.open_reastaurant()

#9-11 导入Admin类
from ch9 import Admin
my_admin = Admin("Bill","Gates")
my_admin.show_privileges()

#9014 骰子
from random import randint
class Die:
def __init__(sides = 6):
self.sides = sides
def roll_die():
return randint(1, self.sides)

my_die = Die(6)
for i in range(1, 10):
print(my_die.roll_die)



阅读更多
想对作者说点什么?

博主推荐

换一批

没有更多推荐了,返回首页