内容概要
-
面向對象的三大特征
1.封裝
2.繼承
3.多态
-
繼承的屬性查找順序
1.單繼承下的屬性查找
2.多繼承下的屬性查找
- super()和mro清單
- 多态與多态性
繼承
1.什麼是繼承
繼承就是建立類的一種方式,建立的類我們稱為子類或派生類,被繼承的類我們稱為父類或基類
子類可以使用父類中的屬性或方法
2.為什麼要用繼承
類解決了對象與對象之間的代碼備援問題
繼承解決了類與類之間的代碼備援問題
3.如何使用繼承
新式類:繼承了object類的子子孫孫類都是新式類
經典類:沒有繼承object類的子子孫孫類都是經典類
新式類和經典類隻有在python2中有區分
類的繼承
# 父類
class People():
school = 'zj'
def __init__(self, name, age, hobby):
self.name = name
self.age = age
self.hobby = hobby
# 學生類(子類)
class Student(People):
def __init__(self, name, age, hobby, course=None):
if course is None:
course = []
People.__init__(self, name, age, hobby)
self.courses = course
def choice_course(self, course):
self.courses.append(course)
print('學生%s選擇%s課程' % (self.name, self.courses))
stu = Student('aaa', 18, 'music')
stu.choice_course('music')
# 教師類(子類)
class Teacher(People):
def __init__(self, name, age, hobby, ):
People.__init__(self, name, age, hobby)
def score(self, stu1, stu2, score):
print('教師%s給%s的%s課程打%s分' % (self.name, stu1, stu2, score))
tea = Teacher('sss', 27, 'run')
tea.score('aaa', 'music', 90)
單繼承下屬性查找
class Animal():
def dog(self):
print('is dog')
def cat(self):
print('is cat')
self.dog()
class Crawl(Animal):
def dog(self):
print('one dog')
obj = Crawl()
obj.cat()
練習
class Animal():
def __dog(self): # _Animal__dog
print('is dog')
def cat(self):
print('is cat')
self.__dog() # _Animal__dog
class Crawl(Animal):
def __dog(self): # _Crawl__dog
print('one dog')
obj = Crawl()
obj.cat()
多繼承下的屬性查找
新式類:按照廣度優先查詢
經典類:按照深度優先查詢
class A():
def test(self):
print('from A')
class B(A):
def test(self):
print('from B')
class C(A):
def test(self):
print('from C')
class D(B):
def test(self):
print('from D')
class E(C):
def test(self):
print('from E')
class F(D, E):
def test(self):
print('from F')
obj = F()
obj.test()
super()和mro()清單
super()用法
# 父類
class People():
school = 'zj'
def __init__(self, name, age, hobby):
self.name = name
self.age = age
self.hobby = hobby
# 學生類(子類)
class Student(People):
def __init__(self, name, age, hobby, course=None):
if course is None:
course = []
# People.__init__(self, name, age, hobby)
# super(Student, self)傳回一個特殊對象
# 他的使用遵循mro清單
super(Student, self).__init__(name, age, hobby)
self.courses = course
def choice_course(self, course):
self.courses.append(course)
print('學生%s選擇%s課程' % (self.name, self.courses))
stu = Student('aaa', 18, 'music')
print(stu.name)
# 教師類(子類)
class Teacher(People):
def __init__(self, name, age, hobby, ):
# People.__init__(self, name, age, hobby)
super().__init__(name, age, hobby)
def score(self, stu1, stu2, score):
print('教師%s給%s的%s課程打%s分' % (self.name, stu1, stu2, score))
tea = Teacher('sss', 27, 'run')
print(tea.age)
mro清單
練習1
class A:
def test(self):
print('from A.test')
super().test()
class B:
def test(self):
print('from B')
class C(A, B):
pass
c = C()
c.test()
print(C.__mro__)
練習2
class A:
def test(self):
print('A---->test')
super().aaa()
class B:
def test(self):
print('B---->test')
def aaa(self):
print('B---->aaa')
class C(A, B):
def aaa(self):
print('C----->aaa')
c = C()
c.test()
print(C.__mro__)
練習3
class A:
def test(self):
print('A---->test')
super().aaa()
class B:
def test(self):
print('B---->test')
def aaa(self):
print('B---->aaa')
class C(A, B):
def aaa(self):
print('C----->aaa')
c = A()
print(A.__mro__)
c.test() # 報錯
什麼事多态
水:液态水,固态水,氣态水
import abc
# 抽象類: 抽象類隻能被繼承,不能被執行個體化
class Animal(metaclass=abc.ABCMeta):
@abc.abstractmethod # 該方法已經是抽象方法了
def speak(self): pass
@abc.abstractmethod
def login(self): pass
class People(Animal):
def speak(self):
# print('嗷嗷嗷')
pass
def login(self):
pass
class Pig(Animal):
def speak(self):
print('哼哼哼')
class Dog(Animal):
def speak(self):
print('汪汪汪')
obj = People()
obj.speak()
# 多态練習
class Pig():
def speak(self):
print('哼哼哼')
class Dog():
def speak(self):
print('汪汪汪')
class Txt():
def speak(self):
print('Txt')
obj = People()
obj1 = Pig()
obj2 = Dog()
obj3 = Txt()
# 多态帶來的特性:在不用考慮對象資料類型的情況下,直接調用對應的函數
def animal(animal):
return animal.speak()
animal(obj)
animal(obj1)
animal(obj2)
animal(obj3)
# 父類限制子類的行為
class Animal():
def speak(self):
raise Exception("必須實作speak方法")