第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定

定義調(diào)用問(wèn)題:為什么set方法不起作用。1.一個(gè)@classmethod下,set在get后,不行;set在get前,可行。2.分別類定義set和get,可行。

class Animal(object):

? ? __count=0

? ? def __init__(self,name,age):

? ? ? ? ?self.name=name

? ? ? ? ?self.age=age


? ? @classmethod

? ??

? ? def get_count(cls):

? ? ? ? return cls.__count

? ? def set_count(cls,count):

? ? ? ? cls.__count=count

? ? ? ??

Leo=Animal('herman',22)

print('name:{}\nage:{}'.format(Leo.name,Leo.age))


print('init count:',Leo.get_count())


Leo.set_count(98)

print('changed count:',Leo.get_count())



正在回答

3 回答

實(shí)例本身無(wú)count,get_count定義的是類方法,因此Leo.get_count()返回Animal的私有屬性__count=0,set_count是實(shí)例方法對(duì)類無(wú)效,因此獲取的__count 還是原本的0.

1 回復(fù) 有任何疑惑可以回復(fù)我~

@classmethod 用于標(biāo)識(shí)緊接著它的那一個(gè)方法

1 回復(fù) 有任何疑惑可以回復(fù)我~

? ? @classmethod

? ? def get_count(cls):

? ? ? ? return cls.__count

? ? ? ?

? ? @classmethod ? ?

? ? def set_count(cls,count):

? ? ? ? cls.__count=count


0 回復(fù) 有任何疑惑可以回復(fù)我~
#1

hermaniu 提問(wèn)者

謝謝你的回答,這是我說(shuō)的第二種方式,分別定義。為什么單獨(dú)定義一個(gè)classmethod標(biāo)識(shí)符,然后再定義兩個(gè)函數(shù),先后會(huì)有區(qū)別
2021-11-19 回復(fù) 有任何疑惑可以回復(fù)我~
#2

weixin_慕圣9105026 回復(fù) hermaniu 提問(wèn)者

這樣的話比如get方法在classmethod下,會(huì)作為類的方法,而set方法會(huì)作為實(shí)例的方法,set更改之后的并不是類中的count,所以你獲取的仍然是最初的
2023-06-09 回復(fù) 有任何疑惑可以回復(fù)我~
#3

weixin_慕圣9105026 回復(fù) hermaniu 提問(wèn)者

相反,count是類中的變量,所以set作為類的方法將count值更改,get也只能獲取的是類中唯一的count值,也就是你set更改的
2023-06-09 回復(fù) 有任何疑惑可以回復(fù)我~

舉報(bào)

0/150
提交
取消

定義調(diào)用問(wèn)題:為什么set方法不起作用。1.一個(gè)@classmethod下,set在get后,不行;set在get前,可行。2.分別類定義set和get,可行。

我要回答 關(guān)注問(wèn)題
微信客服

購(gòu)課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)