继承:实际作用就是对现有的类不去进行改动,对父类的功能在子类中进行功能扩展。
1. 先简单的写一个继承的类型
# -*- coding=utf-8 -*-
class Human(object):
def __init__(self, name, sex):
self.name = name
self.sex = sex
def speak(self):
print(self.name + "can speak")
def __str__(self):
return 'name:%s & age:%s' %(self.name, self.age)
class Man(Human):
def __init__(self, name, age, weight, height, sex = "F"):
# 构造函数的继承写法
super(Man, self).__init__(name,sex) # Human.__init__(self,name,sex)
self.age = age
self.weight = weight
self.height = height
# 方法的重载
def speak(self):
print(self.name + " can speak,shout,cry...")
def __str__(self):
return 'name:%s & age:%d & weight:%d & height:%d & sex:%s' % (self.name, self.age, self.weight, self.height, self.sex)
if __name__ == '__main__':
person_a = Man('wang', 21, 144, 175)
person_a.speak()
print(person_a)
# >>>wang can speak,shout,cry...
# >>>name:wang & age:21 & weight:144 & height:175 & sex:F
"""
以上就是一个简单的类的继承过程
"""
2.子类只能继承父类的公有属性和方法 (如果需要访问的话是需要使用父类中的方法调用父类中的私有属性或者方法)【或者子类自己弄一个私有的属性】
# -*- coding=utf-8 -*-
class Human(object):
def __init__(self, name, sex, age):
self.name = name
self.sex = sex
self.__age = age
def speak(self):
print(self.name + "can speak")
def getAge(self):
print(str(self.__age) + " father class 中的私有属性")
def __str__(self):
return 'name:%s & age:%s' %(self.name, self.__age)
class Man(Human):
def __init__(self, name, age, weight, height, sex = "F"):
super(Man, self).__init__(name, sex, age) # Human.__init__(self,name,sex)
self.weight = weight
self.height = height
self.__age = 22
# 方法的重载
def speak(self):
print(self.name + " can speak,shout,cry...")
def getAge(self):
print(str(self.__age) + " current class 中的私有属性")
def __str__(self):
return 'name:%s & weight:%d & height:%d & sex:%s' % (self.name, self.weight, self.height, self.sex)
if __name__ == '__main__':
person_a = Man('wang', 21, 144, 175)
person_a.speak()
print(person_a)
person_a.getAge()
3. 多继承问题(新式类&经典类区别)
# python3中所有的类都是新式类(哪怕类名后边什么都没有跟,python3自动默认继承object)
新式类中多继承查找使用的式深度查找
# python2中
# 这个叫做经典类:使用的广度查找
class Person:
pass
# 这个叫做新式类:使用的深度查找
class Person(object):
pass
深度查找的例子:
写一个类似上图中的简单例子 :H>>E>>B>>F>>G>>C>>A (深度查找)
# !/usr/bin/env python2.7
# -*- coding=utf-8 -*-
# 新式类下的深度查找
class A(object):
def test(self):
print("AAAAAAAAAAAAA")
class B(A):
def test(self):
print("BBBBBBBBBBBBB")
class C(A):
def test(self):
print("CCCCCCCCCCCCCC")
class D(A):
def test(self):
print("DDDDDDDDDDDDD")
class E(B):
def test(self):
print("EEEEEEEEEEEEEE")
class F(C):
def test(self):
print("FFFFFFFFFFFFFFF")
class G(C):
def test(self):
print("GGGGGGGGGGGGGGG")
class H(E,F,G):
pass
if __name__ == '__main__':
print(H.__mro__)
# >>>> (<class '__main__.H'>, <class '__main__.E'>, <class '__main__.B'>, <class '__main__.F'>, <class '__main__.G'>, <class '__main__.C'>, <class '__main__.A'>, <type 'object'>)
广度优先的顺序:H>>E>>F>>G>>B>>C>>A
多态:一类事物有多种形态,多种形态具体相同的方法 但是方法具体实现不一样,只有在调用的时候才会看到是在使用哪一种形态
# -*- coding=utf-8 -*-
class Car(object):
def __init__(self, name):
self.name = name
def run(self):
print(Car.__name__ + '正在加速中。。。。')
class Motor(Car):
def __init__(self, name):
super(Motor, self).__init__(name)
def run(self):
print(Motor.__name__ + '正在加速中。。。。')
class Electric_Car(Car):
def __init__(self,name):
super(Electric_Car, self).__init__(name)
def run(self):
print(Electric_Car.__name__ + '正在加速中。。。。')
if __name__ == '__main__':
def run_twice(car):
car.run()
run_twice(Car('car'))
run_twice(Electric_Car('Electric_Car'))
run_twice(Motor('Motor'))
"""
Car正在加速中。。。。
Electric_Car正在加速中。。。。
Motor正在加速中。。。。
"""
super函数:前边也涉及到了这部分的内容(这里总的来写一下:主要就是子类调用父类方法的一种写法)
class Car(object):
def __init__(self, name):
self.name = name
def run(self):
print(Car.__name__ + '正在加速中。。。。')
class Motor(Car):
def __init__(self, name):
super(Motor, self).__init__(name)
def run(self):
print(Motor.__name__ + '正在加速中。。。。')
“”“
这里主要是父类的构造函数的调用
在子类构造函数中,先执行父类的这个构造方法
如果自己有特殊的话 就执行自己的
”“”
# -*- coding=utf-8 -*-
class Car(object):
def run(self):
print('%s running' %(Car.__name__))
class Motor(Car):
def run(self):
super(Motor, self).run()
print('%s running' %(Motor.__name__))
if __name__ == '__main__':
m = Motor()
m.run()
"""
父类方法的调用
Car running
Motor running
"""