python封装类在当前文件中使用_name_下调用不了_学python中对于类怎么也不明白,对了是看到简明教程11章,类与变量的对象....想要通俗易懂的答案....
代碼(簡(jiǎn)明python教程第11章,感覺(jué)看懂了大半但是其中我沒(méi)調(diào)用的為什么也有...謝謝!!!):#coding:utf-8#數(shù)據(jù)結(jié)構(gòu)好抽象啊..#python的空格制表位,是災(zāi)難的開(kāi)始啊#init制表位的強(qiáng)制默認(rèn)項(xiàng)了...
代碼(簡(jiǎn)明python教程第11章,感覺(jué)看懂了大半但是其中我沒(méi)調(diào)用的為什么也有...謝謝!!!):#coding:utf-8#數(shù)據(jù)結(jié)構(gòu)好抽象啊..#python的空格制表位,是災(zāi)難的開(kāi)始啊#init制表位的強(qiáng)制默認(rèn)項(xiàng)了#網(wǎng)上查了以下類(lèi)大概是藍(lán)圖類(lèi)似,自行車(chē)?yán)锩婵赡苡懈鞣N牌子型號(hào)的自行車(chē),多態(tài)封裝,面向?qū)ο?...好累的描述class Person: population =0 def __init__ (self,name): #定義一個(gè)對(duì)象'''initializes the person's data.'''self.name=nameprint'(initializing %s)'%self.namePerson.population += 1def __del__(self):'''I am dying.'''print'%s says bye.' %self.namePerson.population -=1if Person.population==0:print'I am the last one.'else:print 'There are still%d people left.'%Person.populationdef sayhi(self):'''Greeting by the person.Really,that's all it does.'''print'Hi,my name is %s.'%self.namedef howmany(self):'''Prints the current population.'''if Person.population==1:print'I am the only person here.'else:print'We have %d persons here'%Person.populationswaroop=Person('swaroop')swaroop.sayhi()swaroop.howmany()kalam=Person('Abdul Kalam')kalam.sayhi()kalam.howmany()swaroop.sayhi()swaroop.howmany()輸出: (initializing swaroop)Hi,my name is swaroop.I am the only person here.(initializing Abdul Kalam)Hi,my name is Abdul Kalam.We have 2 persons hereHi,my name is swaroop.We have 2 persons hereAbdul Kalam says bye.There are still1 people left.swaroop says bye.I am the last one.我應(yīng)該是從2開(kāi)始不能理解了...第一個(gè)數(shù)字加1,為什么第二也加了呢...臣妾做不到啊...被擋了2天了....Σ( ° △ °|||)︴
簡(jiǎn)單來(lái)說(shuō)這一段前面的我都能理解,但是為什么后面又引用 __del__ 的屬性了呢?不懂了...
包括推薦的那個(gè)答案里的我能看懂...但是...下面的我就迷糊了
We have 2 persons here
Hi,my name is swaroop.
We have 2 persons here
Abdul Kalam says bye.
There are still1 people left.
swaroop says bye.
I am the last one.
展開(kāi)
總結(jié)
以上是生活随笔為你收集整理的python封装类在当前文件中使用_name_下调用不了_学python中对于类怎么也不明白,对了是看到简明教程11章,类与变量的对象....想要通俗易懂的答案....的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。