亚洲精品久久久中文字幕-亚洲精品久久片久久-亚洲精品久久青草-亚洲精品久久婷婷爱久久婷婷-亚洲精品久久午夜香蕉

您的位置:首頁技術文章
文章詳情頁

Python基于staticmethod裝飾器標示靜態方法

瀏覽:5日期:2022-07-08 08:49:33

英文文檔:

staticmethod(function)

Return a static method for function.

A static method does not receive an implicit first argument.

The @staticmethod form is a function decorator ? see the description of function definitions in Function definitions for details.

It can be called either on the class (such as C.f()) or on an instance (such as C().f()). The instance is ignored except for its class.

標示方法為靜態方法的裝飾器

說明:

1. 類中普通的方法,實際上既可以被類直接調用也可以被類的實例對象調用,但是被實例對象調用的時候,要求方法至少有一個參數,而且調用時會將實例對象本身傳給第一個參數

>>> class Student(object): def __init__(self,name): self.name = name def sayHello(lang): print(lang) if lang == ’en’: print(’Welcome!’) else: print(’你好!’) >>> Student.sayHello<function Student.sayHello at 0x02AC7810>>>> a = Student(’Bob’)>>> a.sayHello<bound method Student.sayHello of <__main__.Student object at 0x02AD03F0>>>>> Student.sayHello(’en’) # 類調用的時候,將’en’傳給了lang參數enWelcome!>>> a.sayHello() # 類實例對象調用的時候,將對象本身自動傳給了lang參數,不能再接收參數<__main__.Student object at 0x02AD03F0>你好! >>> a.sayHello(’en’) Traceback (most recent call last): File '<pyshell#7>', line 1, in <module> a.sayHello(’en’) TypeError: sayHello() takes 1 positional argument but 2 were given

2. staticmethod函數功能就是將一個方法定義成類的靜態方法,正確的方法是使用 @staticmethod裝飾器,這樣在實例對象調用的時候,不會把實例對象本身傳入靜態方法的第一個參數了。

# 使用裝飾器定義靜態方法>>> class Student(object): def __init__(self,name): self.name = name @staticmethod def sayHello(lang): print(lang) if lang == ’en’: print(’Welcome!’) else: print(’你好!’) >>> Student.sayHello(’en’) #類調用,’en’傳給了lang參數enWelcome!>>> b = Student(’Kim’) #類實例對象調用,不再將類實例對象傳入靜態方法>>> b.sayHello()Traceback (most recent call last): File '<pyshell#71>', line 1, in <module> b.sayHello()TypeError: sayHello() missing 1 required positional argument: ’lang’>>> b.sayHello(’zh’) #類實例對象調用,’zh’傳給了lang參數zh你好!

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持好吧啦網。

標簽: Python 編程
相關文章:
主站蜘蛛池模板: 蜜桃视频一区二区在线观看 | 国产免费观看网站 | 国产999在线| 一级黄色免费网站 | 日本美女视频韩国视频网站免费 | 亚洲国产成人在线观看 | 91一区二区视频 | 亚洲一级毛片 | 91短视频黄色 | 亚洲欧美日韩国产精品久久 | 青青热久久综合网伊人 | 国产小视频在线观看免费 | 国产成人视屏 | 日本aaaa级毛片在线看 | 日韩 欧美 综合 | 黄色小视频在线免费观看 | 九九热国产在线 | a一级爱做片免费观看欧美 a一级黄 | 求一个黄色网址 | 在线观看免费高清激情爱爱 | 国产69页 | yiren22亚洲综合高清一区 | 欧美在线性视频 | 中文字幕啪啪 | 欧美一级毛片国产一级毛片 | 老汉tv永久视频福利在线观看 | 成年人在线观看视频网站 | 中文学幕专区 | 日韩亚洲欧洲在线com91tv | 免费看欧美一级特黄a毛片 免费看欧美一级特黄α大片 | 国产成人午夜精品免费视频 | 国产制服在线 | 国产麻豆精品在线观看 | 国产欧美精品一区二区三区四区 | 国产欧美一区二区精品性色99 | 亚洲欧美久久精品 | 中文字幕在线视频网 | 岛国视频在线观看免费播放 | 中文字幕在线影院 | 五月天婷婷激情网 | 2021最新国产成人精品免费 |