1.类与对象(构造方法与实例化)
#!/usr/bin/env python # _*_ coding:UTF-8 _*_ class Province: def __init__(self, name, capital, leader): self.name = name self.capital = capital self.leader = leader if __name__ == "__main__": hebei = Province("河北", "石家庄", "刘道强") print hebei.name print hebei.capital print hebei.leader
结果:
/Users/liudaoqiang/PycharmProjects/numpy/venv/bin/python /Users/liudaoqiang/Project/python_project/day05/day18/Province.py 河北 石家庄 刘道强 Process finished with exit code 0
注意:定义构造方法使用__init__方法
2.动态字段与静态字段
#!/usr/bin/env python # _*_ coding:UTF-8 _*_ class Province: memo = "中国的23个省之一" def __init__(self, name, capital, leader): self.name = name self.capital = capital self.leader = leader if __name__ == "__main__": hebei = Province("河北", "石家庄", "刘道强") print hebei.name print hebei.capital print hebei.leader print hebei.memo print Province.memo
结果:
/Users/liudaoqiang/PycharmProjects/numpy/venv/bin/python /Users/liudaoqiang/Project/python_project/day05/day18/Province.py 河北 石家庄 刘道强 中国的23个省之一 中国的23个省之一 Process finished with exit code 0
注意:
(1)类不能访问动态字段
(2)对象可以访问静态字段
3.动态方法与静态方法
#!/usr/bin/env python # _*_ coding:UTF-8 _*_ class Province: memo = "中国的23个省之一" def __init__(self, name, capital, leader): self.name = name self.capital = capital self.leader = leader def sports_meeting(self): return self.name + "正在开运动会" @staticmethod def foo(): return "每个省都在反腐" if __name__ == "__main__": hebei = Province("河北", "石家庄", "刘道强") print hebei.name print hebei.capital print hebei.leader print hebei.memo print Province.memo print hebei.sports_meeting() print hebei.foo() print Province.foo()
结果:
/Users/liudaoqiang/PycharmProjects/numpy/venv/bin/python /Users/liudaoqiang/Project/python_project/day05/day18/Province.py 河北 石家庄 刘道强 中国的23个省之一 中国的23个省之一 河北正在开运动会 每个省都在反腐 每个省都在反腐 Process finished with exit code 0
注意:
(1)类不能访问动态方法
(2)对象可以方位静态方法
(3)在动态方法上增加@staticmethod并去除self参数则为动态方法
4.特性
#!/usr/bin/env python # _*_ coding:UTF-8 _*_ class Province: memo = "中国的23个省之一" def __init__(self, name, capital, leader): self.name = name self.capital = capital self.leader = leader def sports_meeting(self): return self.name + "正在开运动会" @staticmethod def foo(): return "每个省都在反腐" @property def bar(self): return self.name if __name__ == "__main__": hebei = Province("河北", "石家庄", "刘道强") print hebei.name print hebei.capital print hebei.leader print hebei.memo print Province.memo print hebei.sports_meeting() print hebei.foo() print Province.foo() print hebei.bar
结果:
/Users/liudaoqiang/PycharmProjects/numpy/venv/bin/python /Users/liudaoqiang/Project/python_project/day05/day18/Province.py 河北 石家庄 刘道强 中国的23个省之一 中国的23个省之一 河北正在开运动会 每个省都在反腐 每个省都在反腐 河北 Process finished with exit code 0
注意:
(1)在方法上增加@property则该方法变为字段访问
5.面向对象中动态方法与静态方法的比较
(1)使用动态方法适合于业务类;适用于对象数量较少的情况
(2)使用静态方法适合于工具类;适用于对象数量较多的情况,当对象较多则创建对象占用大量的内存这时应该使用静态方法
6.私有字段与私有方法
#!/usr/bin/env python # _*_ coding:UTF-8 _*_ class Province: memo = "中国的23个省之一" def __init__(self, name, capital, leader, __thailand): self.name = name self.capital = capital self.leader = leader self.__thailand = __thailand def sports_meeting(self): return self.name + "正在开运动会" @staticmethod def foo(): return "每个省都在反腐" @property def bar(self): return self.name @property def thailand(self): return self.__thailand if __name__ == "__main__": hebei = Province("河北", "石家庄", "刘道强", True) print hebei.name print hebei.capital print hebei.leader print hebei.memo print Province.memo print hebei.sports_meeting() print hebei.foo() print Province.foo() print hebei.bar print hebei.thailand
结果:
/Users/liudaoqiang/PycharmProjects/numpy/venv/bin/python /Users/liudaoqiang/Project/python_project/day05/day18/Province.py 河北 石家庄 刘道强 中国的23个省之一 中国的23个省之一 河北正在开运动会 每个省都在反腐 每个省都在反腐 河北 True Process finished with exit code 0
注意:
(1)任何暴露给外部是无用的,或者为了安全不能暴露给外部的字段和方法需要设置为私有;设置私有字段和私有方法只需在前边加上两个下划线即可
(2)私有字段在类内部可以直接访问,在类外部不能访问,可以通过实现特性来访问私有字段
(3)私有方法在类内部可以直接访问,在类外部不能访问,可以通过封装共有方法来访问私有方法
7.只读特性与可写特性
#!/usr/bin/env python # _*_ coding:UTF-8 _*_ class Province: memo = "中国的23个省之一" def __init__(self, name, capital, leader, __thailand): self.name = name self.capital = capital self.leader = leader self.__thailand = __thailand def sports_meeting(self): return self.name + "正在开运动会" @staticmethod def foo(): return "每个省都在反腐" @property def bar(self): return self.name @property def thailand(self): return self.__thailand @thailand.setter def thailand(self, __thailand): self.__thailand = __thailand if __name__ == "__main__": hebei = Province("河北", "石家庄", "刘道强", True) print hebei.name print hebei.capital print hebei.leader print hebei.memo print Province.memo print hebei.sports_meeting() print hebei.foo() print Province.foo() print hebei.bar print hebei.thailand hebei.thailand = False print hebei.thailand
结果:
/Users/liudaoqiang/PycharmProjects/numpy/venv/bin/python /Users/liudaoqiang/Project/python_project/day05/day18/Province.py 河北 石家庄 刘道强 中国的23个省之一 中国的23个省之一 河北正在开运动会 每个省都在反腐 每个省都在反腐 河北 True False Process finished with exit code 0
/Users/liudaoqiang/PycharmProjects/numpy/venv/bin/python /Users/liudaoqiang/Project/python_project/day05/day18/Province.py 河北 石家庄 刘道强 中国的23个省之一 中国的23个省之一 河北正在开运动会 每个省都在反腐 每个省都在反腐 河北 True False Process finished with exit code 0
注意
(1)使用@property装饰器则私有字段可读;使用@field.setter则私有字段可写
8.构造方法__init__(),析构方法__del__()和__call__()
#!/usr/bin/env python # _*_ coding:UTF-8 _*_ class Province: memo = "中国的23个省之一" def __init__(self, name, capital, leader, __thailand): self.name = name self.capital = capital self.leader = leader self.__thailand = __thailand def __call__(self, *args, **kwargs): print "CALL" def __del__(self): print "DELETE" def sports_meeting(self): return self.name + "正在开运动会" @staticmethod def foo(): return "每个省都在反腐" @property def bar(self): return self.name @property def thailand(self): return self.__thailand @thailand.setter def thailand(self, __thailand): self.__thailand = __thailand if __name__ == "__main__": hebei = Province("河北", "石家庄", "刘道强", True) print hebei.name print hebei.capital print hebei.leader print hebei.memo print Province.memo print hebei.sports_meeting() print hebei.foo() print Province.foo() print hebei.bar print hebei.thailand hebei.thailand = False print hebei.thailand hebei()
结果:
/Users/liudaoqiang/PycharmProjects/numpy/venv/bin/python /Users/liudaoqiang/Project/python_project/day05/day18/Province.py 河北 石家庄 刘道强 中国的23个省之一 中国的23个省之一 河北正在开运动会 每个省都在反腐 每个省都在反腐 河北 True False CALL DELETE Process finished with exit code 0
注意:
__init__(self)在创建对象的时候触发调用
__del__(self)在销毁对象的时候触发调用
__call__(self)在对象()调用时触发调用
9.类的继承
#!/usr/bin/env python # _*_ coding:UTF-8 _*_ class Base: def __int__(self): pass def __call__(self, *args, **kwargs): pass def __del__(self): pass def foo(self): print "foo" def bar(self): print "bar" class Derive(Base): def __int__(self): pass def __call__(self, *args, **kwargs): pass def __del__(self): pass def bar(self): Base.bar(self) print "derive" if __name__ == "__main__": base = Base() base.foo() base.bar() derive = Derive() derive.foo() derive.bar()
结果:
/Users/liudaoqiang/PycharmProjects/numpy/venv/bin/python /Users/liudaoqiang/Project/python_project/day05/day18/extend_test.py foo bar foo bar derive Process finished with exit code 0
注意:
(1)继承只需要的类名的后面加上基类的名称
(2)调用基类的方法则用Base.bar(self)
10. 多继承
经典类的深度优先多继承:
#!/usr/bin/env python # _*_ coding:UTF-8 _*_ class A: def __init__(self): print "init A" def save(self): print "save from A" class B(A): def __init__(self): print "init B" class C(A): def __init__(self): print "init C" def save(self): print "save from C" class D(B, C): def __init__(self): print "init D" if __name__ == "__main__": d = D() d.save()
结果:
/Users/liudaoqiang/PycharmProjects/numpy/venv/bin/python /Users/liudaoqiang/Project/python_project/day05/day18/object_test.py init D save from A Process finished with exit code 0
新式类的广度优先多继承:
#!/usr/bin/env python # _*_ coding:UTF-8 _*_ class A(object): def __init__(self): print "init A" def save(self): print "save from A" class B(A): def __init__(self): print "init B" class C(A): def __init__(self): print "init C" def save(self): print "save from C" class D(B, C): def __init__(self): print "init D" if __name__ == "__main__": d = D() d.save()
结果:
/Users/liudaoqiang/PycharmProjects/numpy/venv/bin/python /Users/liudaoqiang/Project/python_project/day05/day18/object_test.py init D save from C Process finished with exit code 0
注意:
(1)python中的类如果不继承object则为经典类,如果继承object类则为新式类
(2)经典类的多继承采用深度优先算法继承,新式类的多继承采用广度优先算法继承
11.接口,抽象类,抽象方法
例如:
#!/usr/bin/env python # _*_ coding:UTF-8 _*_ from abc import ABCMeta, abstractmethod class Foo: __metaclass__ = ABCMeta def save(self): pass class Bar(Foo): def __init__(self): print "init Bar" def save(self): print "save from Bar" if __name__ == "__main__": bar = Bar() bar.save()
结果:
/Users/liudaoqiang/PycharmProjects/numpy/venv/bin/python /Users/liudaoqiang/Project/python_project/day05/day18/abstract_test.py init Bar save from Bar Process finished with exit code 0
注意:
(1)抽象类中只有抽象方法即为借口
(2)类中有__metaclass__ = ABCMeta的类即为抽象类;方法被@abstractmethod装饰即为抽象方法