1. 程式人生 > >python練習---建立類以及例項

python練習---建立類以及例項

建立一個名為Restaurant 的類,其方法__init__() 設定兩個屬性:name 和type 。建立一個名為describe_restaurant() 的方法和一個名為open_restaurant() 的方法,其中前者列印前述兩項資訊,而後者列印一條訊息,指出餐館正在營業。根據這個類建立一個名為a的例項,分別列印其兩個屬性,再呼叫前述兩個方法。

 

# coding=gbk
class Resturant():
    def __init__(self,name,type):
        self.name=name
        self.type=type
    def describe_resturant(self):
        print("餐館名稱為:"+self.name)
        print("餐館型別為:"+self.type)
    def open_resturant(self):
        print("正在營業!!")

a=Resturant('hh','g')
a.describe_resturant()
a.open_resturant()


輸出:
餐館名稱為:hh
餐館型別為:g
正在營業!!