class fruits():
def apple(self,name):
print("我是%s"%(name))
def method(self,name,func):
return func(name)
def main(self):
return self.method("紅蘋果",self.apple)
fruits().main()
關鍵在於method()
方法裡返回的函數func()
已經是實例化的了,所以傳參不能加self
from datetime import datetime
class fruits():
def method(f):
def x(self,*args,**kwargs):
print(datetime.now())
f(self,*args,**kwargs)
return;
return x
@method
def apple(self,name):
print("我是%s"%(name))
fruits().apple("青蘋果")