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. VMware空虚拟机通过网络安装系统时获取不到IP地址情况(基于Linux的DHCP服务器)

    当虚拟机提示no DHCP or proxyDHCP offers were received时 1.dhcp服务配置错误(留意句尾分号“  :”) 2.dhcp服务未启动(用 systemctl s ...

  2. 关于singer elt 的几篇很不错的文章

    以下是链接来自singer 团队的实践,很不错,值得学习 参考连接 https://www.stitchdata.com/blog/100-billion-records-later-refining ...

  3. system.stat[resource,<type>]

    系统信息. 整型或者浮点型 ent - 该分区有权接收的处理器单元数(float) kthr, - 关于内核线程状态的信息: r - 平均可运行内核线程数(float) b - 虚拟内存管理器等待队列 ...

  4. shell脚本获取传入参数的个数

    ts.sh #!/bin/bash echo $# 输出 [root@redhat6 ~]# ./ts.sh para1 [root@redhat6 ~]# ./ts.sh para1 para2 [ ...

  5. [CMS]凡诺cms 2.1文件包含

    0x01 简介 凡诺CMS下载链接:凡诺企业网站管理系统PHP 2.1 安装好了是这样的: 0x02 漏洞复现 在添加频道处上传附件: 根据网站根目录所在位置用../进行跨目录: 然后返回首页点击频道 ...

  6. 获取Android包名和activity名

    个人主要用2个方法. 方法1:pm list package 方法2: windows:adb shell logcat | findstr START; linux: adb shell logca ...

  7. 责任链模式(chainOfResponsibility)

    参考文章:http://wiki.jikexueyuan.com/project/design-pattern-behavior/chain-four.html 定义: 使多个对象都有机会处理请求,从 ...

  8. jvm (一)jvm结构 & 类加载 & 双亲委托模型

    参考文档: jvm内幕-java虚拟机详解:http://www.importnew.com/17770.html 常量池:https://www.jianshu.com/p/c7f47de2ee80 ...

  9. 高通qxdm抓取sensor的log【学习笔记】

    高通qxdm抓取sensor的log 打开qxdm,打开设置界面,去掉其他无关的log,打开Log packets .Message packets的SNS的log 之后需要把端口打开,把端口打开之后 ...

  10. Matlab Script to pre-process UAV123 tracking dataset

    Matlab Script to pre-process UAV123 tracking dataset 2019-11-08 09:43:11 Official project page: http ...