Python编程从入门到实践 第九章:类 练习答案记录
创始人
2024-04-09 20:51:58

Python编程从入门到实践 第九章:类 练习答案记录

练习题导航

  • Python编程从入门到实践 第九章:类 练习答案记录
  • 9.1 创建和使用类
    • 9.1.1 创建Dog类
    • 9.1.2 根据类创建实例
      • 练习9-1 餐馆 : 创建一个名为Restaurant 的类, 其方法__init__() 设置两个属性: restaurant_name 和cuisine_type 。 创建一个名为describe_restaurant() 的方法和一个名为open_restaurant() 的方法, 其中前者打印前述两项信息, 而后者打印一条消息, 指出餐馆正在营业。根据这个类创建一个名为restaurant 的实例, 分别打印其两个属性, 再调用前述两个方法。
      • 练习9-2 三家餐馆 : 根据你为完成练习9-1而编写的类创建三个实例, 并对每个实例调用方法describe_restaurant() 。
      • 练习9-3 用户 : 创建一个名为User 的类, 其中包含属性first_name 和last_name , 还有用户简介通常会存储的其他几个属性。 在类User 中定义一个名为describe_user() 的方法, 它打印用户信息摘要; 再定义一个名为greet_user() 的方法, 它向用户发出个性化的问候。
  • 9.2 使用类和实例
    • 9.2.1 Car类
    • 9.2.2 给属性指定默认值
    • 9.2.3 修改属性的值
      • 练习9-4 就餐人数 : 在为完成练习9-1而编写的程序中, 添加一个名为number_served 的属性, 并将其默认值设置为0。
      • 练习9-5 尝试登录次数 : 在为完成练习9-3而编写的User 类中, 添加一个名为login_attempts 的属性。
  • 9.3 继承
    • 9.3.1 子类的方法__init__()
    • 9.3.2 给子类定义属性和方法
    • 9.3.3 重写父类的方法
    • 9.3.4 将实例用作属性
    • 9.3.5 模拟实物

9.1 创建和使用类

9.1.1 创建Dog类

9.1.2 根据类创建实例

练习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_nameself.cuisine_type=cuisine_typedef describe_restaurant(self):print(f"restaurant_name:{self.restaurant_name}")print(f"cuisine_type:{self.cuisine_type}")def open_restaurant(self):print("餐馆正在营业")restaurant=Restaurant('A','a')
restaurant.describe_restaurant()
restaurant.open_restaurant()

在这里插入图片描述

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

class Restaurant:def __init__(self,restaurant_name,cuisine_type):self.restaurant_name=restaurant_nameself.cuisine_type=cuisine_typedef describe_restaurant(self):print(f"restaurant_name:{self.restaurant_name}")print(f"cuisine_type:{self.cuisine_type}")def open_restaurant(self):print("餐馆正在营业")a=Restaurant('A','a')
b=Restaurant('B','b')
c=Restaurant('C','c')
a.describe_restaurant()
b.describe_restaurant()
c.describe_restaurant()

在这里插入图片描述

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

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

class User:def __init__(self,first_name,last_name):self.first_name=first_nameself.last_name=last_namedef describe_user(self):print(self.first_name)print(self.last_name)def greet_user(self):print("Hello~")a=User('A','a')
a.describe_user()
a.greet_user()

在这里插入图片描述

9.2 使用类和实例

9.2.1 Car类

9.2.2 给属性指定默认值

9.2.3 修改属性的值

练习9-4 就餐人数 : 在为完成练习9-1而编写的程序中, 添加一个名为number_served 的属性, 并将其默认值设置为0。

根据这个类创建一个名为restaurant 的实例; 打印有多少人在这家餐馆就餐过, 然后修改这个值并再次打印它。
添加一个名为set_number_served() 的方法, 它让你能够设置就餐人数。 调用这个方法并向它传递一个值, 然后再次打印这个值。
添加一个名为increment_number_served() 的方法, 它让你能够将就餐人数递增。 调用这个方法并向它传递一个这样的值:你认为这家餐馆每天可能接待的就餐人数。

1、根据这个类创建一个名为restaurant 的实例; 打印有多少人在这家餐馆就餐过

class Restaurant:def __init__(self,restaurant_name,cuisine_type):self.restaurant_name=restaurant_nameself.cuisine_type=cuisine_typeself.number_served=0def describe_restaurant(self):print(f"restaurant_name:{self.restaurant_name}")print(f"cuisine_type:{self.cuisine_type}")def open_restaurant(self):print("餐馆正在营业")
restaurant=Restaurant('A','a')
print(f"多少人在这家餐馆就餐过:{restaurant.number_served}")

在这里插入图片描述

2、根据这个类创建一个名为restaurant 的实例,修改这个值并再次打印它。

class Restaurant:def __init__(self,restaurant_name,cuisine_type):self.restaurant_name=restaurant_nameself.cuisine_type=cuisine_typeself.number_served=0def describe_restaurant(self):print(f"restaurant_name:{self.restaurant_name}")print(f"cuisine_type:{self.cuisine_type}")def open_restaurant(self):print("餐馆正在营业")
restaurant=Restaurant('A','a')
restaurant.number_served=10
print(f"多少人在这家餐馆就餐过:{restaurant.number_served}")

在这里插入图片描述

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

class Restaurant:def __init__(self,restaurant_name,cuisine_type):self.restaurant_name=restaurant_nameself.cuisine_type=cuisine_typeself.number_served=0def describe_restaurant(self):print(f"restaurant_name:{self.restaurant_name}")print(f"cuisine_type:{self.cuisine_type}")def open_restaurant(self):print("餐馆正在营业")def set_number_served(self,number_served):self.number_served=number_servedrestaurant=Restaurant('A','a')
restaurant.set_number_served(11)
print(f"多少人在这家餐馆就餐过:{restaurant.number_served}")

在这里插入图片描述

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

class Restaurant:def __init__(self,restaurant_name,cuisine_type):self.restaurant_name=restaurant_nameself.cuisine_type=cuisine_typeself.number_served=0def describe_restaurant(self):print(f"restaurant_name:{self.restaurant_name}")print(f"cuisine_type:{self.cuisine_type}")def open_restaurant(self):print("餐馆正在营业")def set_number_served(self,number_served):self.number_served = number_serveddef increment_number_served(self,digit):self.number_served += digitrestaurant=Restaurant('A','a')
restaurant.set_number_served(11)
print(f"多少人在这家餐馆就餐过:{restaurant.number_served}")
restaurant.increment_number_served(10)
print(f"多少人在这家餐馆就餐过:{restaurant.number_served}")

在这里插入图片描述

练习9-5 尝试登录次数 : 在为完成练习9-3而编写的User 类中, 添加一个名为login_attempts 的属性。

编写一个名为increment_login_attempts() 的方法,它将属性login_attempts 的值加1。
再编写一个名为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_nameself.last_name=last_nameself.login_attempts=0def describe_user(self):print(self.first_name)print(self.last_name)def greet_user(self):print("Hello~")def increment_login_attempts(self):self.login_attempts+=1def reset_login_attempts(self):self.login_attempts=0user=User('A','a')
user.increment_login_attempts()
user.increment_login_attempts()
user.increment_login_attempts()
print(user.login_attempts)
user.reset_login_attempts()
print(user.login_attempts)

在这里插入图片描述

9.3 继承

9.3.1 子类的方法__init__()

9.3.2 给子类定义属性和方法

9.3.3 重写父类的方法

9.3.4 将实例用作属性

9.3.5 模拟实物

相关内容

热门资讯

埃菲尔铁塔在哪 中国仿建埃菲尔... 2019年4月26日,广西南宁市,街头惊现一座巨型山寨版埃菲尔铁塔,高约20米,白色塔身,造型逼真,...
苗族的传统节日 贵州苗族节日有... 【岜沙苗族芦笙节】岜沙,苗语叫“分送”,距从江县城7.5公里,是世界上最崇拜树木并以树为神的枪手部落...
北京的名胜古迹 北京最著名的景... 北京从元代开始,逐渐走上帝国首都的道路,先是成为大辽朝五大首都之一的南京城,随着金灭辽,金代从海陵王...
长白山自助游攻略 吉林长白山游... 昨天介绍了西坡的景点详细请看链接:一个人的旅行,据说能看到长白山天池全凭运气,您的运气如何?今日介绍...
应用未安装解决办法 平板应用未... ---IT小技术,每天Get一个小技能!一、前言描述苹果IPad2居然不能安装怎么办?与此IPad不...
脚上的穴位图 脚面经络图对应的... 人体穴位作用图解大全更清晰直观的标注了各个人体穴位的作用,包括头部穴位图、胸部穴位图、背部穴位图、胳...
猫咪吃了塑料袋怎么办 猫咪误食... 你知道吗?塑料袋放久了会长猫哦!要说猫咪对塑料袋的喜爱程度完完全全可以媲美纸箱家里只要一有塑料袋的响...
demo什么意思 demo版本... 618快到了,各位的小金库大概也在准备开闸放水了吧。没有小金库的,也该向老婆撒娇卖萌服个软了,一切只...
世界上最漂亮的人 世界上最漂亮... 此前在某网上,选出了全球265万颜值姣好的女性。从这些数量庞大的女性群体中,人们投票选出了心目中最美...
埃菲尔铁塔在哪 中国仿建埃菲尔... 2019年4月26日,广西南宁市,街头惊现一座巨型山寨版埃菲尔铁塔,高约20米,白色塔身,造型逼真,...
苗族的传统节日 贵州苗族节日有... 【岜沙苗族芦笙节】岜沙,苗语叫“分送”,距从江县城7.5公里,是世界上最崇拜树木并以树为神的枪手部落...
北京的名胜古迹 北京最著名的景... 北京从元代开始,逐渐走上帝国首都的道路,先是成为大辽朝五大首都之一的南京城,随着金灭辽,金代从海陵王...
长白山自助游攻略 吉林长白山游... 昨天介绍了西坡的景点详细请看链接:一个人的旅行,据说能看到长白山天池全凭运气,您的运气如何?今日介绍...
世界上最漂亮的人 世界上最漂亮... 此前在某网上,选出了全球265万颜值姣好的女性。从这些数量庞大的女性群体中,人们投票选出了心目中最美...
应用未安装解决办法 平板应用未... ---IT小技术,每天Get一个小技能!一、前言描述苹果IPad2居然不能安装怎么办?与此IPad不...
脚上的穴位图 脚面经络图对应的... 人体穴位作用图解大全更清晰直观的标注了各个人体穴位的作用,包括头部穴位图、胸部穴位图、背部穴位图、胳...
demo什么意思 demo版本... 618快到了,各位的小金库大概也在准备开闸放水了吧。没有小金库的,也该向老婆撒娇卖萌服个软了,一切只...
猫咪吃了塑料袋怎么办 猫咪误食... 你知道吗?塑料袋放久了会长猫哦!要说猫咪对塑料袋的喜爱程度完完全全可以媲美纸箱家里只要一有塑料袋的响...