面向對象的特徵之繼承

  • 2021 年 12 月 6 日
  • 筆記

今日內容概要

  • 面向對象的三大特徵
    • 封裝
    • 繼承(重要)
    • 多態
  • 繼承的屬性查找順序
    • 單繼承下的屬性查找
    • 多繼承下的屬性查找
  • super()和mro()列表
  • 多態與多態性(了解)

image

內容詳細

封裝

# 將功能封裝成函數

# 在類中 我們正常定義的方法 就是封裝

image

繼承(概念)

1.什麼是繼承
	'''
	繼承就是新建類的一種方式 新建的類我們稱之為 子類 或者 派生類
	被繼承的類稱之為 父類 或者 基類
		子類可以使用父類中的屬性或方法
	'''
    
2.為什麼要用繼承
	'''
	類解決了對象與對象之間的程式碼冗餘問題
    繼承解決的是類與類之間的程式碼冗餘問題
    '''
3.如何使用繼承
	# 新式類
    	繼承了object類的子子孫孫類
    # 經典類
    	沒有繼承object類的子子孫孫類
    '''
    新式類和經典類只在python2中區分
    python3隻有新式類
    '''

image

繼承實例

# 以學生選課系統為例

# 定義父類
class People:
    school = 'SH'
    def __init__(self, name, age, gender):
        self.name = name
        self.age = age
        self.gender = gender
        
# 定義學生類(子類)
class Student(People):  # 繼承父類People

    def __init__(self, name, age, gender, course=None):
        if course is None:
            course = []
        People.__init__(self, name, age, gender)
        self.courses = course

    def choose_course(self, course):
        self.courses.append(course)
        print('%s 選課成功 %s' % (self.name, self.courses))

stu = Student('ly', 19, 'male')
print(stu.name)  # ly

# 定義老師類(子類)
class Teacher(People):  # 繼承父類People

    def __init__(self, name, age, gender, level):
        self.level = level
        People.__init__(self, name, age, gender)

    def score(self, stu_obj, score):
        stu_obj.score = score  # 給學生打分
        print('%s給%s打了%s分' % (self.name, stu_obj.name, score))

tea = Teacher('ly', 19, 'male', 10)
print(tea.name)  # ly
print(tea.level)  # 10

image

單繼承下的屬性查找

class Foo:
    def f1(self):
        print('Foo.f1')

    def f2(self):
        print('Foo.f2')  # 1
        self.f1()  # self = obj = Bar 所以要找 Bar中的 f1

class Bar(Foo):
    def f1(self):
        print('Bar.f1')  # 2

obj = Bar()  # {}
print(Bar.mro())  # [<class '__main__.Bar'>, <class '__main__.Foo'>, <class 'object'>] 查找空間順序
obj.f2()
'''
列印結果:
    Foo.f2
    Bar.f1
'''


# 練習
class Foo:
    def __f1(self):  # _Foo__f1()
        print('Foo.f1')

    def f2(self):
        print('Foo.f2')  # 1
        self.__f1()  # _Foo__f1() 此處的__f1是屬於Foo類空間產生的f2對象空間中 所以結果是 Foo.f1

class Bar(Foo):
    def __f1(self):  # # _Bar__f1()
        print('Bar.f1')

obj = Bar()  # {}
print(Bar.mro())  # [<class '__main__.Bar'>, <class '__main__.Foo'>, <class 'object'>] 查找空間順序
obj.f2()
'''
列印結果:
    Foo.f2
    Foo.f1
'''

image

多繼承下的屬性查找

# 新式類:按照廣度優先查詢(經典類:按照深度優先查詢)
class A(object):
    def test(self):
        print('from A')

class B(A):
    def test(self):
        print('from B')
    pass

class C(A):
    def test(self):
        print('from C')
    pass

class D(B):
    # def test(self):
    #     print('from D')
    pass

class E(C):
    # def test(self):
    #     print('from E')
    pass

class F(D, E):
    # def test(self):
    #     print('from F')
    pass

f1 = F()
f1.test()
print(F.mro())  # 查找順序按照 [<class '__main__.F'>, <class '__main__.D'>, <class '__main__.B'>, <class '__main__.E'>, <class '__main__.C'>, <class '__main__.A'>, <class 'object'>] 找不到就下一個類 不能返回查找
'''
列印結果:
	from B
'''

image

super()和mro列表

# 父類
class People():
    school = 'SH'

    def __init__(self, name, age, gender):
        self.name = name
        self.age = age
        self.gender = gender

# 學生類(子類)
class Student(People):
    def __init__(self, name, age, gender, course=None):
        if course is None:
            course = []
        # People.__init__(self, name, age, gender)  # 指名道姓的調用父類的方法
        # super(Student, self) 返回了一個特殊的對象
        # 它的使用遵從mro列表
        # super(Student, self).__init__(name, age, gender)  # python2的寫法
        super().__init__(name, age, gender)  # python3 的寫法
        self.courses = course
        
    def choose_course(self, course):
        self.courses.append(course)
        print('%s 選課成功 %s' % (self.name, self.courses))

stu = Student('ly', 19, 'male')
print(stu.name)  # ly

# 老師類(子類)
class Teacher(People):
    def __init__(self, name, age, gender, level):
        self.level = level
        People.__init__(self, name, age, gender)

    def score(self, stu_obj, score):
        stu_obj.score = score  # 給學生打分
        print('%s給%s打了%s分' % (self.name, stu_obj.name, score))

tea = Teacher('ly', 19, 'male', 10)
print(tea.name)  # ly
print(tea.level)  # 10


# 例題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__)  # 查找順序 按照mro列表 由C3線性演算法產生 (<class '__main__.C'>, <class '__main__.A'>, <class '__main__.B'>, <class 'object'>)
'''
列印結果:
	from A.test
	from B
'''


# 例題2
class B:
    def test(self):
        print('B---->test')

    def aaa(self):
        print('B---->aaa')

class A:
    def test(self):
        print('A---->test')
        super().aaa()

class C(A, B):
    def aaa(self):
        print('C----->aaa')

c = A()
c.test()  # 列印結果: A---->test + 報錯
print(A.mro())  # 查找順序 [<class '__main__.A'>, <class 'object'>] A類找不到就直接報錯

image

多態和多態性(了解)

1.什麼是多態
	水:
        液態水 固態水 氣態水
    動物:
        人 豬 狗 貓...
        
# 抽象類 定義
	抽象類只能被繼承 不能被實例化
    
# 固定語法
import abc

class Animal(metaclass=abc.ABCMeta):
    @abc.abstractmethod  # 該方法已經是抽象方法了
    def speak(self): pass

    @abc.abstractmethod  # 子類函數必須含有 login 方法 沒有就會直接報錯
    def login(self): pass
    
class People(Animal):
    def speak(self):
        print('嗷嗷嗷')
        
    def login(self):  # 子類函數必須含有 login 方法 沒有就會直接報錯
        pass
    
'''
如果定義抽象類了 那麼子類當中必須由抽象類的方法名
即使子類函數體程式碼為pass也可
否則將直接報錯 而不會再到抽象類(父類)查找
eg:
class People(Animal):
	def jiao(self):
    	print('嗷嗷嗷')

obj = People()
obj.speak()
列印結果:
	直接報錯
'''

class Pig(Animal):
    def speak(self):
        print('哼哼哼')

class Dog(Animal):
    def speak(self):
        print('汪汪汪')

obj = People()
obj.speak()
'''
列印結果:
    嗷嗷嗷
'''   



# 在python3中 抽象類寫法墨守成規為以下
# 省去了abc模組固定語法
class People():
    def speak(self):
        print('嗷嗷嗷')

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)



# len 就是多態一種場景
len('abc')
len([1, 2, 3])
len({'username': 'ly'})

def len(item):
    return item.__len__()
print(len('abc'))




# 父類限制子類的行為(抽象類) 還可以用異常捕獲主動報錯
class Animal():
    def speak(self):
        raise Exception("必須實現speak方法")  # 主動報錯出去
 
class People(Animal):
    pass
 
class Pig():
    def speak(self):
        print('哼哼哼')
 
class Dog():
    def speak(self):
        print('汪汪汪')
        
obj = People()
obj.speak()



# 一切皆文件 只要類中含有read,write方法 就看做文件類
class Txt:
    def read(self):
        pass

    def write(self):
        pass

class Process():
    def read(self):
        pass

    def write(self):
        pass

image