1、创建类时init函数是两个下划线
2、访问属性和访问方法的example
class Restaurant():
def __init__(self,restaurant_name,cuisine_type):
self.restaurant_name=restaurant_name
self.cuisine_type=cuisine_type
def describe_restaurant(self):
print('this restaurant is called '+self.restaurant_name.title())
print("it's cuisine type is "+self.cuisine_type.title())
def open_restaurant(self):
print(self.restaurant_name+'is open,welcome!')
#访问属性
my_restaurant=Restaurant("li's restaurant","chinese")
print('my restaurant is '+my_restaurant.restaurant_name)
print("it's cuisine type is "+my_restaurant.cuisine_type.title())
print(my_restaurant.restaurant_name+'is open,welcome!\n')
#访问方法
my_restaurant.describe_restaurant()
my_restaurant.open_restaurant()
output:
my restaurant is li's restaurant it's cuisine type is Chinese li's restaurantis open,welcome! this restaurant is called Li'S Restaurant it's cuisine type is Chinese li's restaurantis open,welcome!