14天Python编程从入门到实践--Day11:类

#9.1 9.2
class Restaurant():
# __inin__ 是两个下划线。。。不是一个
#    def _init_(self,restaurant_name,cuisine_type):
    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 + "是一家" + self.cuisine_type + "餐厅")
    def open_restaurant(self):
        print(self.restaurant_name + "餐厅正在营业")

xfy = Restaurant("小肥羊","火锅")
xfy.describe_restaurant()
xfy.open_restaurant()

hdl = Restaurant("海底捞","火锅")
hdl.describe_restaurant()

kdj = Restaurant("肯德基","快餐")
kdj.describe_restaurant()

输出:
小肥羊是一家火锅餐厅
小肥羊餐厅正在营业

#9.3
class User():
    def __init__(self, first_name, last_name, gender="男",education="本科"):
        self.first_name = first_name
        self.last_name = last_name
        self.gender = gender
        self.education = education
    def describe_user(self):
        msg = self.first_name + self.last_name + "个人信息:"
        msg += "\n性别:" + self.gender
        msg += "\n学历:" + self.education
        print(msg)
    def greet_user(self):
        print("你好呀" + self.first_name + self.last_name)
        
yuminhong = User("敏洪","俞")
daqing = User("青","大","女","硕士")
zhangshaohan = User("韶涵","张","女","高中")

yuminhong.describe_user()
yuminhong.greet_user()
daqing.describe_user()
daqing.greet_user()
zhangshaohan.describe_user()
zhangshaohan.greet_user()

输出:
敏洪俞个人信息:
性别:男
学历:本科
你好呀敏洪俞
青大个人信息:
性别:女
学历:硕士
你好呀青大
韶涵张个人信息:
性别:女
学历:高中
你好呀韶涵张
#9.4 需求描述不清
class Restaurant():
    def __init__(self, restaurant_name, cuisine_type, number_served=0):
        self.restaurant_name = restaurant_name
        self.cuisine_type = cuisine_type
        self.number_served = number_served
    def describe_restaurant(self):
        print(self.restaurant_name + "是一家" + self.cuisine_type + "餐厅")
    def open_restaurant(self):
        print(self.restaurant_name + "餐厅正在营业")
    def set_number_served(self,num_served):
        if num_served >= self.number_served:
            self.number_served = num_served
            print("有" + str(self.number_served) + "顾客在" + self.restaurant_name + "就餐过")
        else:
            print("有" + str(self.number_served) + "顾客在" + self.restaurant_name + "就餐过"
                  + "不可能只有" + num_served + "顾客")
    def increment_number_served(self,inc_served):
        self.number_served += inc_served
        print(self.number_served)

输出:
有200顾客在老北京炸酱面就餐过
220





你可能感兴趣的:(14天Python编程从入门到实践--Day11:类)