python面向对象编程学习[1]

根据代码进行学习

class Province:           #定义了一个类
    momo = '中国的23个省之一'  # 字段

    def __init__(self, name, city, leader, flag):  # 构造器方法
        self.Name = name
        self.City = city
        self.Leader = leader
        self.__Flag = flag  # 私有字段

    def sport_game(self):  # 动态方法
        print self.Name + '在开运动会'

    @staticmethod  # 静态方法
    def foo(self):
        print '每个省都要好好发展'

    @property  # 特性
    def bar(self):
        print self.Name

    def show(self):
        print self.__Flag

    def __me(self):
        print 'i am zty'

    def me(self):
        self.__me()

gs = Province('甘肃', '兰州', 'zty', True)          #实例化这个类

print gs.momo
print gs.Name
print gs.City
print gs.sport_game()
Province.foo(gs)
gs.bar

gs.me()
gs._Province__me()
'''



'''
class tyzZ:
    def __init__(self):      #构造函数
        pass

    def __del__(self):      #析构函数
        print '销毁'

    def go(self):
        print 'go'
    def __call__(self, *args, **kwargs):        #call方法
        print 'call'

zty = tyzZ()            #实例化这个类

zty()

zty.go()
'''

'''
class father():                 #父类(基类)
    def __init__(self):
        self.Fname = 'name'
    def func(self):
        print 'father.func'

    def bad(self):
        print 'father smoke drink'

class son(father):         #子类(派生类)

    def __init__(self,name):
        self.Sname = name
    def qwe(self):
        print 'son.func'
    def bad(self):
        father.bad(self)



s = son('ssssname')

s.func()

s.qwe()

s.bad()
posted @ 2016-07-10 21:31  tyzZ001  阅读(160)  评论(0编辑  收藏  举报