说明:学习资料来自于《Python编程:从入门到实践》,如果想深入学习可以去买这本书看看
1.类的说明:
1.1基于类创建对象,每个对象都自动具备这种的通用行为;
1.2根据类来创建对象被称为实例化;
1.3类中的函数被称为方法
2.实例
#2.1 创建一个名为Restaurant的类
class Restaurant():
"""设置属性"""
def __init__(self,name,type):
self.name=name
self.type=type
"""创建describe_restaurant方法"""
def describe_restaurant(self):
print("\n"+self.name.title())
print(self.type.title())
"""创建open_restaurant方法"""
def open_resaurant(self):
print("This restaurant is open now.")
sichuan_restaurant=Restaurant('honey_well','sichuan_food')
sichuan_restaurant.describe_restaurant()
sichuan_restaurant.open_resaurant()
hunan_restaura