关于类的tips
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!《新程序員》:云原生和全面數字化實踐50位技術專家共同創作,文字、視頻、音頻交互閱讀
總結
- 上一篇: 关于for循环处理列表的思考
- 下一篇: 单链表基本操作(可执行程序),二级指针使