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 = True
    huge = '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 = 2
    name = ['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

posted @ 2015-08-09 22:44  笑面浮屠  阅读(236)  评论(0编辑  收藏  举报