9.Python基础 面向对象的进一步拓展
生活随笔
收集整理的這篇文章主要介紹了
9.Python基础 面向对象的进一步拓展
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
vamei前輩的博客講解的很詳細,博客下面的討論給人以啟發:
http://www.cnblogs.com/vamei/archive/2012/06/02/2532018.html
講解了類的__init__()函數,相當于c++的構造函數,講解了類的屬性和對象的性質的區別(看著像c++中類的靜態屬性和普通屬性間的區別):
#!/usr/bin/python ''' class Human(object):joker = 'It\'t me'def show_joker(self):print (self.joker) #self相當于c++中的this指針,只是這里要顯示表示def joker_10th(self):for i in range(10):self.show_joker()man = Human() man.show_joker() man.joker_10th() ''' class Bird(object):have_feather = Truehuge = 'big or small'class HappyBird(Bird):def __init__(self, more_words): #在構造對象的時候會調用__init__()函數print ('We are happy birds,', more_words) #相當于c++中的構造函數summer = HappyBird('happy!happy!')''' class Human(object):joker = 'It\'s me' #類屬性,不分私有公有?都是公有?def __init__(self, input_gender):self.gender = input_gender #gender對象性質def printGender(self):print (self.gender) li_lei = Human('male') li_lei.printGender() print (li_lei.joker) print (li_lei.gender) ''' class Human(object): #和上面有什么區別呢?joker = 'It\'s me' #類屬性,不分私有公有?都是公有?age = 5 #整型、字符串等相當于一個平常的屬性grade = 2name = ['zhang', 'san'] #list列表相當于一個類的靜態屬性def __init__(self, input_gender, input_name):self.gender = input_gender #gender對象性質self.tempName = input_name #而此時并不知道tempName為一個列表,估計它會像整型一樣處理def printGender(self):print (self.gender) li_lei = Human('male', ['li', 'lei']) han_mm = Human('female', ['han', 'mm']) li_lei.age = 6 li_lei.grade = 6 han_mm.age = 8 han_mm.grade = 8 li_lei.name[0] = 'li' han_mm.name[1] = 'mm' li_lei.printGender() han_mm.printGender() print ('li_lei:age =', li_lei.age, ',grade =', li_lei.grade,\'name =', li_lei.name, 'temName =', li_lei.tempName) #li_lei:age = 6 ,grade = 6 name = ['li', 'mm'] temName = ['li', 'lei'] print ('han_mm:age =', han_mm.age, 'grade =', han_mm.grade,\'name =', han_mm.name, 'tempName =', han_mm.tempName) #han_mm:age = 8 grade = 8 name = ['li', 'mm'] tempName = ['han', 'mm'] print (Human('male', ['li', 'lei']).name) #['li', 'mm'] li_lei.tempName[0] = 'xx' han_mm.tempName[1] = 'yy' print (li_lei.tempName) #['xx', 'lei'] print (han_mm.tempName) #['han', 'yy']print (li_lei.__class__.__dict__) ''' {'__init__': <function Human.__init__ at 0x02426CD8>, '__doc__': None, 'age': 5, 'grade': 2, 'name': ['li', 'mm'], '__dict__':<attribute '__dict__' of 'Human' objects>, '__module__': '__main__','__weakref__': <attribute '__weakref__' of 'Human' objects>,'printGender': <function Human.printGender at 0x02426D20>,'joker': "It's me"} ''' print (han_mm.__dict__) #{'gender': 'female', 'tempName': ['han', 'yy'], 'grade': 8, 'age': 8}class man(Human):hello = 'xx'xiao_li = man('male', 'xiao_li') xiao_li.printGender() #male print (xiao_li.tempName) #xiao_li</pre><pre name="code" class="python">輸出結果: We are happy birds, happy!happy!male
female
li_lei:age = 6 ,grade = 6 name = ['li', 'mm'] temName = ['li', 'lei']
han_mm:age = 8 grade = 8 name = ['li', 'mm'] tempName = ['han', 'mm']
['li', 'mm']
['xx', 'lei']
['han', 'yy']
{'joker': "It's me", '__module__': '__main__', '__weakref__': <attribute '__weakref__' of 'Human' objects>, '__doc__': None, 'printGender': <function Human.printGender at 0x023D0228>, 'grade': 2, '__init__': <function Human.__init__ at 0x023B1618>, 'name': ['li', 'mm'], 'age': 5, '__dict__': <attribute '__dict__' of 'Human' objects>}
{'grade': 8, 'age': 8, 'tempName': ['han', 'yy'], 'gender': 'female'}
male
xiao_li
轉載于:https://www.cnblogs.com/v-BigdoG-v/p/7398642.html
總結
以上是生活随笔為你收集整理的9.Python基础 面向对象的进一步拓展的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 天津鑫茂工业园区------------
- 下一篇: websocket python爬虫_p