首页 > 编程语言 > 详细

python 入门到实践第九章课后练习

时间:2020-06-28 17:22:00      阅读:64      评论:0      收藏:0      [点我收藏+]

class Restaurant():#创建一个类
def __init__(self,restaurant_name,cuisine_type):#定义初始化属性,2个形参
self.restaurant_name = restaurant_name#通过self 赋值给方法
self.cuisine_type = cuisine_type

def describe_restaurant(self):#定义另一个方法
print(self.restaurant_name)
print(self.cuisine_type)
def open_restaurant(self):
print(self.restaurant_name + " is runging")


my_restaurant = Restaurant(‘alex‘,‘rung‘)#创建一个实例,虽然类没有return ,但但Python自动返回一个表示这个的实例
print(my_restaurant.restaurant_name)
print(my_restaurant.cuisine_type)
my_restaurant.describe_restaurant()#创建一个实例后,可以用.来调用方法
my_restaurant.open_restaurant()


one_restaurant = Restaurant(‘one‘,‘close‘)
one_restaurant.describe_restaurant()

two_restaurant = Restaurant(‘two‘,‘aaa‘)
two_restaurant.describe_restaurant()

shere_restaurant = Restaurant(‘shere‘,‘asdqwe‘)
shere_restaurant.describe_restaurant()

class User():
def __init__(self,first_name,ast_name,age):
self.first_name = first_name
self.ast_name = ast_name
self.abb = age

def describe_user(self):
print(self.first_name)
print(self.abb)


myname = User(‘alex‘,‘wei‘,31,)

myname.describe_user()

print(myname.first_name + " " + myname.ast_name)

python 入门到实践第九章课后练习

原文:https://www.cnblogs.com/upuser/p/13203921.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!