首页 > 其他 > 详细

第9章 类

时间:2021-05-20 22:22:24      阅读:12      评论:0      收藏:0      [点我收藏+]

1. 动手试一试

技术分享图片

 

 2. 代码

class Restaurant():
    def __init__(self, restaurant_name, cuisine_type):
        self.restaurant_name = restaurant_name
        self.cuisine_type = cuisine_type
        self.number_served = 0  # 添加属性,设置默认值为0

    def describle_restaurant(self):
        long_name = self.restaurant_name + " have "                     + str(self.cuisine_type) + " kinds of food."
        return long_name.title()

    def open_restaurant(self):
        print("Now is opening...")

    def read_restaurant(self):
        print("The restaurant have " + str(self.number_served) + ‘ people eating.‘)

    def update_restaurant(self, numbers):
        self.number_served = numbers
        if numbers >= self.number_served:
            self.number_served = numbers
        else:
            print("The reserve is full.")

    def set_number_served(self, member):    # 添加方法,设置就餐人数
        self.number_served = member

    def increment_number_served(self, members): # 添加方法,设置递增人数
        self.number_served += members

restaurant = Restaurant(‘Kaoyu‘,10) # 创建实例
print(restaurant.describle_restaurant())    # 打印餐厅信息
restaurant.read_restaurant()

restaurant.update_restaurant(20)
restaurant.read_restaurant()

restaurant.set_number_served(30)    # 调用方法,传值打印
restaurant.read_restaurant()

restaurant.increment_number_served(100)
restaurant.read_restaurant()
print(">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>")


class User():   # 创建User类
    def __init__(self, first_name, last_name, age, address, phone): # 属性
        self.first_name = first_name
        self.last_name = last_name
        self.age = age
        self.address = address
        self.phone = phone
        self.login_attempts = 0 # 添加属性

    def describe_user(self):    # 方法
        print(self.first_name,
              self.last_name,
              self.age,
              self.address,
              self.phone)

    def greet_user(self):   # 方法
        print("How beautiful name " + self.last_name + self.last_name,
              "\n too young, too simple", "your homeland " + self.address
              + " is a warm place, ", "could you tell me your contact?")

    def increment_login_attempts(self, number1):    # 递增方法
        self.login_attempts += number1  # 值加1

    def reset_login_attempts(self, number2):    # 重置方法,重置为0
        self.login_attempts = 0

user = User(‘Michile‘, ‘Jadon‘, 40, ‘Chicago‘, 10089)   # 创建实例
user.increment_login_attempts(1)    #调用递增方法
print(user.login_attempts)  #打印值

user.reset_login_attempts(0) # 调用重置方法
print(user.login_attempts)

user.increment_login_attempts(1)    #调用递增
user.increment_login_attempts(1)    #再次调用
user.increment_login_attempts(1)    #第三次调用,此时值应该为3(每次递增1)
print(user.login_attempts)

  3. 运行结果

D:\python编程:从入门到实践\venv\Scripts\python.exe D:/python编程:从入门到实践/Restaturant02.py
Kaoyu Have 10 Kinds Of Food.
The restaurant have 0 people eating.
The restaurant have 20 people eating.
The restaurant have 30 people eating.
The restaurant have 130 people eating.
>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
1
0
3

Process finished with exit code 0

  

第9章 类

原文:https://www.cnblogs.com/kevin-hou1991/p/14791204.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!