#!/usr/bin/env python
# _*_ coding:utf-8 _*_
# 面向对象
'''
class Province:
province = '23个省之一' # 静态字段
# __init__ 是构造函数
def __init__(self, name, capital, leader, flag):
# 动态字段
self.Name = name
self.Capital = capital
self.Leader = leader
self.__Thailand = flag # 私有字段
def __del__(self):
print '析构函数'
# 动态方法
def sport_meet(self, game):
print "%s要举办%s比赛" % (self.Name, game)
def show(self):
print self.__Thailand
print self.__sha()
# 静态方法
@staticmethod
def gdp(name):
return name + '要增长GDP30%'
# 特性
@property
def foo(self):
print self.Name + ' 特性'
# 私有方法
def __sha(self):
return '私有方法'
def __call__(self, *args, **kwargs):
print 'call'
GD = Province('广东', '广州', '网易', True)
print GD.Capital
print GD.Leader
print GD.Name
GD.sport_meet('篮球')
# GD.province = 'aaa'
print GD.province # 不推荐这样访问
print Province.province
print GD.gdp(GD.Name) # 不推荐这样访问
print Province.gdp(GD.Name)
GD.foo
# print GD.__Thailand不能这样直接访问私有字段和方法
GD.show()
GD()
'''
# 类的继承
# 类分为金典类和新式类(继承object)
# 金典类继承深度优先
class A:
# class A(object): 加上object即为新式类
def __init__(self):
print "This is A"
def func(self):
print 'This func is from class A'
class B(A):
def __init__(self):
print "This is B"
class C(A):
def __init__(self):
print "This is C"
def func(self):
print 'This func is from class C'
class D(B, C):
def __init__(self):
print "This is D"
# 由于金典类继承深度优先D将会继承A的func而不会继承C的func(金典类的bug)
# 新式类将不会有此问题,一般都用新式类
d = D()
d.func()
# 接口 = 抽象类+抽象方法
# 继承接口的类必须具体实现接口里定义的方法否则会报错
from abc import ABCMeta, abstractmethod
class Alert:
__metaclass__ = ABCMeta
@abstractmethod
def find(self):
pass
class Foo(Alert):
def __init__(self):
print 'class Foo'
def find(self):
print 'find'
f = Foo()