Python编程:从入门到实践(课后习题:第9章 类)

coding=gbk

coding:utf-8!

chapter 9 practice

9-1餐馆

class Restaurant():
“”“简单创建一个名为restaurant的类”""
def init(self, name, type):
“”“设置name和type两个属性”""
self.name = name
self.type = type
def describe(self):
print(“The restaurant’s name is " + self.name.title()
+ " and it has " + self.type + " cuisine.”)
def open(self):
print(“The restaurant " + self.name.title()
+ " is now opening.”)
restaurant_1 = Restaurant(‘nayuki’, ‘western’) # 别忘了:实例=类()
restaurant_1.describe() # 注意这里实例与方法的位置:实例.方法
restaurant_1.open()

网上参考答案

print(“网上参考答案”)
class Restaurant():
def init(self,restaurant_name,cuisine_type):
“”“初始化属性restaurant_name和cuisine_type”""
self.restaurant_name = restaurant_name
self.cuisine_type = cuisine_type
def describe_restaurant(self):
print(self.restaurant_name)
print(self.cuisine_type)
def open_restaurant(self):
print(‘Restaurant is open’)
restaurant = Restaurant(‘A’,‘B’)
print(restaurant.restaurant_name,’’,restaurant.cuisine_type)
restaurant.describe_restaurant()
restaurant.open_restaurant()

9-2三家餐馆

print("\n三家餐馆")
class Restaurant():
“”“简单创建一个名为restaurant的类”""
def init(self, name, type):
“”“设置name和type两个属性”""
self.name = name
self.type = type
def describe(self):
print(“The restaurant’s name is " + self.name.title()
+ " and it has " + self.type + " cuisine.”)
def open(self):
print(“The restaurant " + self.name.title()
+ " is now opening.”)
restaurant_1 = Restaurant(‘nayuki’, ‘Western’)
restaurant_2 = Restaurant(‘papa john’, ‘Italian’)
restaurant_3 = Restaurant(‘taco’, ‘Spanish’)
restaurant_1.describe()
restaurant_1.open()
restaurant_2.describe()
restaurant_2.open()
restaurant_3.describe()
restaurant_3.open()

9-3用户

print("\n用户")
class User():
def init(self, first_name, last_name):
self.first_name = first_name
self.last_name = last_name
def describe(self):
print("First name: " + self.first_name.title())
print("Last name: " + self.last_name.title())
def great_user(self):
print("Hello " + self.first_name.title() + " "
+ self.last_name.title() + “.”)
user_1 = User(‘tina’, ‘wang’)
user_2 = User(‘john’, ‘hu’)
user_3 = User(‘joseph’, ‘hu’)
user_1.describe()
user_1.great_user()
user_2.describe()
user_2.great_user()
user_3.describe()
user_3.great_user()

9-4就餐人数

print

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值