1. 程式人生 > >python 面向對象與裝飾器

python 面向對象與裝飾器

improve def elf 世界 sel light 裝飾器 return make

面向對象

類(class):現實世界中一些事物的封裝(如:學生)

類:屬性(如:姓名,成績)

類對象

實例對象

引用:通過引用對類的屬性和方法進行操作

實例化:創建一個類的具體實例對象(如:學生張三)

class Student:
    def __init__(self,name,grade):
        self.name = name
        self.grade = grade
    def introduce(self):
        print("hi , i‘m " + self.name)
        print("my grade is :" + str(self.grade))
    def improve(self,amount):
        self.grade = self.grade +amount

  

裝飾器(decorator)

Code:

def add_cand(cake_f):
    def insert_cand():
        return  cake_f() +" and cand"
    return insert_cand()
@add_cand
def make_cake():
    return "cake"

print(make_cake())

  

python 面向對象與裝飾器