《Python编程:从入门到实践》第9章-类 习题

9-1 餐馆

创建一个名为Restaurant的类,其方法__init__()设置两个属性:restaurant_name和cuisine_type。创建一个名为describe_restaurant()的方法和一个名为open_restaurant()的方法,其中前者打印前述两项信息,而后者打印一条消息,指出餐馆正在营业。
根据这个类创建一个名为restaurant的实例,分别打印其两个属性,再调用前述的两个方法。

class Restaurant():
	"""docstring for Restaurant"""
	
	def __init__(self, restaurant_name, cuisine_type):
		self.name = restaurant_name
		self.type = cuisine_type
		#self.name---变量(可以通过实例访问的变量称为属性)
		#restaurant_name---形参

	def describe_restaurant(self):
		print("The restaurant's name is " + self.name.title())
		print("The restaurant has " + str(self.type) + " cuisine types.")
		#该方法打印了前述两项信息

	def open_restaurant(self):
		print("The restaurant is open now.")
		#该方法打印一条消息,指出餐馆正在营业

restaurant = Restaurant('mamala', 10)
print(restaurant.name.title() + ' ' + str(restaurant.type)) 
#打印其两个属性

restaurant.describe_restaurant()
restaurant.open_restaurant()
#根据要求调用前述两个方法。

区分变量和实参:以self为前缀的变量都可供类中的所有方法使用。还可以通过类的任何实例来访问这些变量。

self.name = restaurant_name获取储存在形参restaurant_name中的值,并将其储存到变量name中,然后该变量被关联到当前创建的实例。

像这样可通过实例访问的变量称为属性

build

Mamala 10
The restaurant's name is Mamala
The restaurant has 10 cuisine types.
The restaurant is open now.
[Finished in 2.0s]

9-2 三家餐馆

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

类的主体不变 增加两个实例 代码如下

restaurant_a = Restaurant('mamala', 10)
restaurant_b = Restaurant("jason's library", 8)
restaurant_c = Restaurant("bread talk", 4)

restaurant_a.describe_restaurant()
restaurant_b.describe_restaurant()
restaurant_c.describe_restaurant()

build


The restaurant's name is Mamala
The restaurant has 10 cuisine types.

The restaurant's name is Jason'S Library
The restaurant has 8 cuisine types.

The restaurant's name is Bread Talk
The restaurant has 4 cuisine types.
[Finished in 0.5s]

9-3用户

创建一个名为User的类,其中包含属性first_name和last_name,还有用户简介通常会储存的其他几个属性。在类User定义一个名为describe_user()的方法,它打印用户信息的摘要;再定义一个名为greet_user()的方法,它向用户发出个性化的问候。
创建多个表示不同用户的实例,并对每个实例都调用上述两个方法。

class User():

	def __init__(self, first_name, last_name, age, career):
		self.f_name = first_name
		self.l_name = last_name
		self.age = age
		self.career = career

	def describe_user(self):
		print("\nThe user's name is " + self.f_name.title() + self.l_name.title() + ".")
		print("Age is " + str(self.age) + ".")
		print("The user's career is " + self.career.title() + ".")

	def greet_user(self):
		print("Hello," + self.f_name.title() + " " + self.l_name.title() + ".")

user_a = User('wang', 'xizhi', 25, 'calligrapher' )
user_b = User('Xie', 'an', 40, 'polician')
user_c = User('Xie', 'lingyun', 37, 'poet')

user_a.describe_user()
user_a.greet_user()

user_b.describe_user()
user_b.greet_user()

user_c.describe_user()
user_c.greet_user()

build


The user's name is WangXizhi.
Age is 25.
The user's career is Calligrapher.
Hello,Wang Xizhi.

The user's name is XieAn.
Age is 40.
The user's career is Polician.
Hello,Xie An.

The user's name is XieLingyun.
Age is 37.
The user's career is Poet.
Hello,Xie Lingyun.
[Finished in 0.1s]

9-4 就餐人数

(1&#x

  • 2
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值