python cls参数_定义类方法中的参数为啥是cls
class?people:
country?=?'china'
#類方法,用classmethod來進行修飾????@classmethod????def?getCountry(cls):????????return?cls.country
p?=?people()print?p.getCountry()????#可以用過實例對象引用print?people.getCountry()????#可以通過類對象引用
類方法還有一個用途就是可以對類屬性進行修改:class?people:
country?=?'china'
#類方法,用classmethod來進行修飾????@classmethod????def?getCountry(cls):????????return?cls.country
@classmethod????def?setCountry(cls,country):
cls.country?=?country
p?=?people()print?p.getCountry()????#可以用過實例對象引用print?people.getCountry()????#可以通過類對象引用p.setCountry('japan')
print?p.getCountry()
print?people.getCountry()
運行結果:china
china
japan
japan
總結
以上是生活随笔為你收集整理的python cls参数_定义类方法中的参数为啥是cls的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 汽车照明全国产化电子元件推荐方案
- 下一篇: 用友u8 如何配置文件服务器,用友u8服