9-1 餐馆:创建一个名为Restaurant的类,其方法__init__()设置两个属性:restaurant_name和cuisine_type。创建一个名为describe_restaurant()的方法和一个名为open_restaurant()的方法,其中前者打印前述两项消息,而后者打印一条消息,指出餐馆正在营业。

  根据这个类创建一个名为restaurant的实例,分别打印其两个属性,在调用前述的两个方法。

class Restaurant:
def __init__(self, restaurant_name, cuisine_type):
"""初始化属性"""
self.restaurant_name = restaurant_name
self.cuisine_type = cuisine_type def describe_restaurant(self):
"""打印餐馆的名字和类型"""
print("Restaurant Name: " + self.restaurant_name.title())
print("Cuisine Type: " + self.cuisine_type.title()) def open_restaurant(self):
"""说明餐馆正在营业"""
print("Welcome, our restaurant is open.") restaurant = Restaurant("big bowl noodle", "chinese")
print("Our restaurant'name is " + restaurant.restaurant_name.title() + ".")
print("We often " + restaurant.cuisine_type.title() + " type foods.")
restaurant.describe_restaurant()
restaurant.open_restaurant()

9-2 三家餐馆:根据9-1编写的类创建三个实例,并对每个实例调用方法describe_restaurant()。

restaurant1 = Restaurant("future future", "japanese")
restaurant1.describe_restaurant() restaurant2 = Restaurant("Kyo-Chon", "korean type")
restaurant2.describe_restaurant() restaurant3 = Restaurant("houcaller", "western type")
restaurant3.describe_restaurant()

9-3 用户:创建一个名为User的类,其中包含属性first_name和last_name,还有用户简介通常会存储其他几个属性。在类User中定义一个名为describe——user()的方法,它打印用户信息摘要;再定义一个名为greet_user()的方法,它向用户发出个性化的问候。

  创建多个表示不同用户的实例,并对每个实例都调用上述的两种方法。

class User:
def __init__(self, first_name, last_name, age, address):
"""初始化用户的属性"""
self.first_name = first_name
self.last_name = last_name
self.age = age
self.address = address def describe_user(self):
"""打印用户信息摘要"""
name = self.first_name.title() + " " + self.last_name.title()
print('\n' + name + " " + self.age + " years old!")
print("Live in " + self.address.title() + ".") def greet_user(self):
"""向用户问好"""
name = self.first_name.title() + " " + self.last_name.title()
print("Hello, " + name + "!") user1 = User('shirley', 'yang', '', "xi'an")
user1.describe_user()
user1.greet_user() user2 = User('lucky', 'liu', '', "beijin")
user2.describe_user()
user2.greet_user() user3 = User('suns', 'zhang', '', "shanghai")
user3.describe_user()
user3.greet_user()

9-4 就餐人数:在为9-1编写的程序中,添加一个名为number_served的属性,并将其默认值设置为0。根据这个类创建一个名为restaurant的实例;打印有多少人在这家餐馆就餐过,然后修改这个值并再次打印它。

  添加一个名为set_number_served()的方法,它让你能够设置就餐人数。调用这个方法并向它传递一个值,然后再次打印这个值。

  添加一个名为increment_served()的方法,它让你能够将就餐人数递增。调用这个方法并向它传递一这样的值:你认为这家餐馆每天可能接待的就餐人数。

class Restaurant:
def __init__(self, restaurant_name, cuisine_type):
"""初始化属性"""
self.restaurant_name = restaurant_name
self.cuisine_type = cuisine_type
self.number_served = 0 def describe_restaurant(self):
"""打印餐馆的名字和类型"""
print("Restaurant Name: " + self.restaurant_name.title())
print("Cuisine: " + self.cuisine_type.title()) def read_number_served(self):
"""在餐馆就餐过的人数"""
print("Our served " + str(self.number_served) + " people.\n") def set_number_served(self, number):
"""设置就餐人数"""
self.number_served = number def increment_number_served(self, increment_number):
"""递增就餐人数"""
self.number_served = self.number_served + increment_number restaurant = Restaurant("big bowl noodle", "chinese")
restaurant.describe_restaurant()
restaurant.read_number_served() # 打印默认就过餐人数 restaurant.number_served = 200 # 直接修改就过餐人数
restaurant.read_number_served() # 打印修改后的就过餐的人数 restaurant.set_number_served(60) # 调用方法设置就餐的人数
restaurant.read_number_served() # 打印调用方法修改后的就过餐的人数 restaurant.increment_number_served(140) # 调用方法增加就餐人数
restaurant.read_number_served() # 打印递增后就过餐的人数

9-5 尝试登陆次数:在9-3编写的User类中,添加一个名为login_attempts的属性。编写一个名为increment_login_attempts()的方法,它将属性login_attempts的值加。再编写一个名为reset_login_attempts()的方法,它将属性login_attempts的值重置为0.

  根据User类创建一个实例,再调用方法increment_login_attempts()多次。打印属性login_attempts的值,确认它被正确地递增;然后,调用方法reset_login_attempts(),并再次打印属性login_attempts的值,确认它被重置为0。

class User:
def __init__(self, first_name, last_name):
"""初始化用户的属性"""
self.first_name = first_name
self.last_name = last_name
self.login_attempts = 0 def greet_user(self):
"""向用户问好"""
name = self.first_name.title() + " " + self.last_name.title()
print("Hello, " + name + "!") def increment_login_attempts(self):
"""增加登录次数"""
self.login_attempts = self.login_attempts + 1 def reset_login_attempts(self):
"""重置登录次数"""
self.login_attempts = 0 user = User('mark', 'sun')
user.greet_user()
# 调用方法递增登录次数
for i in range(5):
user.increment_login_attempts()
print("You have login " + str(user.login_attempts) + " times.") # 验证登录次数被正确递增
user.reset_login_attempts() # 调用方法重置登录次数
print("You login " + str(user.login_attempts) + " time.") # 确认登录次数被重置

9-6 冰淇淋小店:冰淇淋小店是一种特殊的餐馆。编写一个名为IceCreamStand的类,让它继承9-1或9-4编写的Restaurant类。这两个版本的Restaurant类都可以,选一个即可。添加一个名为flavors的属性,用于存储一个由各种口味的冰淇淋组成的列表。编写一个显示这些冰淇淋的方法。创建一个IceCreamStand实例,并调用这个方法。

class IceCreamStand(Restaurant):
def __init__(self, restaurant_name, cuisine_type):
"""初始化父类的所有属性"""
super().__init__(restaurant_name, cuisine_type)
self.flavors = ['Green tea', 'rum', 'mango', 'vanilla', 'strawberry', 'chocolate'] def show_icecream_flavors(self):
"""显示店中所有冰淇淋口味"""
print("We provide following flavors IceCream:")
for flavor in self.flavors:
print(flavor.title()) icecreamstand = IceCreamStand('love', 'icecream')
icecreamstand.describe_restaurant()
icecreamstand.show_icecream_flavors()

类的练习——python编程从入门到实践的更多相关文章

  1. Python编程从入门到实践笔记——类

    Python编程从入门到实践笔记——类 #coding=gbk #Python编程从入门到实践笔记——类 #9.1创建和使用类 #1.创建Dog类 class Dog():#类名首字母大写 " ...

  2. Python编程从入门到实践

    Python编程从入门到实践1 起步2 变量和简单数据类型3 列表简介4 操作列表5 if语句6 字典7 用户输入和while循环8 函数9 类10 文件和异常11 测试代码12 武装飞船13 外星人 ...

  3. 《Python编程:从入门到实践》分享下载

    书籍信息 书名:<Python编程:从入门到实践> 原作名:Python Crash Course 作者: [美] 埃里克·马瑟斯 豆瓣评分:9.1分(2534人评价) 内容简介 本书是一 ...

  4. 《python编程从入门到实践》读书实践笔记(二)

    本文是<python编程从入门到实践>读书实践笔记11章的内容,主要包含测试,为体现测试的重要性,独立成文. 11 测试代码 写在前面的话,以下是我这些年开发中和测试相关的血泪史. 对于一 ...

  5. 《python编程从入门到实践》读书实践笔记(一)

    本文是<python编程从入门到实践>读书实践笔记1~10章的内容,主要包含安装.基础类型.函数.类.文件读写及异常的内容. 1 起步 1.1 搭建环境 1.1.1 Python 版本选择 ...

  6. Python编程从入门到实践笔记——异常和存储数据

    Python编程从入门到实践笔记——异常和存储数据 #coding=gbk #Python编程从入门到实践笔记——异常和存储数据 #10.3异常 #Python使用被称为异常的特殊对象来管理程序执行期 ...

  7. Python编程从入门到实践笔记——文件

    Python编程从入门到实践笔记——文件 #coding=gbk #Python编程从入门到实践笔记——文件 #10.1从文件中读取数据 #1.读取整个文件 file_name = 'pi_digit ...

  8. Python编程从入门到实践笔记——函数

    Python编程从入门到实践笔记——函数 #coding=gbk #Python编程从入门到实践笔记——函数 #8.1定义函数 def 函数名(形参): # [缩进]注释+函数体 #1.向函数传递信息 ...

  9. Python编程从入门到实践笔记——用户输入和while循环

    Python编程从入门到实践笔记——用户输入和while循环 #coding=utf-8 #函数input()让程序暂停运行,等待用户输入一些文本.得到用户的输入以后将其存储在一个变量中,方便后续使用 ...

随机推荐

  1. 鸿蒙OS

    8月9日,华为消费者业务在其全球开发者大会上正式发布其全新的基于微内核的面向全场景的分布式操作系统——鸿蒙OS(HarmonyOS)! 鸿蒙的定义是基于微内核的全场景分布式操作系统.其中,微内核是技术 ...

  2. 抽样方法(Sampling Method)

    概率抽样方法: 1. 随机抽样(random sampling):从有限总体中简单随机抽样或从无限总体中随机抽样. 具体实现方式:a. 抽签法:b. 随机数字法 2. 分层抽样(stratified ...

  3. 【JZOJ6231】【20190625】等你哈苏德

    题目 有\(m\)条线段,每条线段为\([l_i,r_i]\),每条线段可以是黑/白色 有些线段已经被染色,有些需要被确定颜色 询问是否存在一种染色方案,使得对于每一个位置\(i\),覆盖它的线段黑白 ...

  4. 【ARC098F】Donation

    [ARC098F]Donation 题面 atcoder 题意: 给定一张\(n\)个点,\(m\)条边的无向图.这张图的每个点有两个权值 \(a_i,b_i\). 你将会从这张图中选出一个点作为起点 ...

  5. Python研究

    听说Python是种高级语言,故打算研究一下,看看高级在哪 学习资源:https://www.liaoxuefeng.com/wiki/1016959663602400 由于上面所示的网站对Pytho ...

  6. nginx 反向代理之 proxy_cache

    proxy_cache将从C上获取到的数据根据预设规则存放到B上(内存+磁盘)留着备用,A请求B时,B会把缓存的这些数据直接给A,而不需要再去向C去获取. proxy_cache相关功能生效的前提是, ...

  7. Exploiting ConvNet Diversity for Flooding Identification

    语义分割洪水区域. 空洞卷积和反卷积组合,结果再用svm学习如何组合,能获得更好的效果. 直接对不同网络的结果进行投票会得到更差的结果. 消融研究(Ablation Study):类似控制变量法,就对 ...

  8. DDD 全称 “Domain-Driven Design”,领域驱动设计

    大型软件项目的最复杂之处不是实现,而是软件所服务的真正的领域. 领域驱动设计就是用来处理这些高度复杂领域的理想和途径,使得领域本身成为项目关注的焦点,从而达到维护能深刻反映领域的软件模型的目的. 通过 ...

  9. html上传文件限制、前端限制文件类型

    <input id="file" type="file" accept=".xls,.xlsx" style="width: ...

  10. [Beta]Scrum Meeting#4

    github 本次会议项目由PM召开,时间为5月9日晚上10点30分 时长15分钟 任务表格 人员 昨日工作 下一步工作 木鬼 撰写博客整理文档 撰写博客整理文档 swoip 改进界面 改进界面 bh ...