python-類與繼承

類的繼承

什麼是繼承?

繼承是一種新建類的方式,新建的類稱爲子類,被繼承的類稱爲父類。python中,父類、子類(派生類)只有在繼承的時候纔會產生。python

繼承的特性:子類會繼承父類全部的屬性。linux

爲何要用繼承?

使用繼承能夠減小代碼的冗餘。函數

對象的繼承

  1. python中支持一個類同時繼承多個父類(不推薦使用,當繼承多個父類的時候,功能與功能之間會出現混亂。)code

  2. 使用__bases__方法能夠獲取對象繼承的類對象

    class A:
        def __init__(self,a):
            self.a =a
    
    class B:
        def __init__(self,b):
            self.b =b
    
    class C(A,B):
        pass
    
    print(C.__bases__)
    
    (<class '__main__.A'>, <class '__main__.B'>)
  3. 在python3中若是一個類沒有繼承任何類,則默認繼承object類繼承

  4. 在python2中若是一個類沒有繼承任何類,不會繼承object類it

對象查找屬性的順序

對象自己--->對象的類--->父類--->父類的父類,若是還沒找到就會報錯class

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

    def f2(self):  # self = b
        print('Foo.f2')
        self.f1()  # b.f1() 


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


b = Bar()
b.f2()  # Foo.f2

Foo.f2
Bar.f1

類的派生

派生:子類中添加新的屬性得過程,這個過程就叫作派生;注意的是,在添加新的屬性同時也會繼承父類全部的東西。object

派生方法一

  1. 相似於函數調用方法

    class Animal:
        def __init__(self,height,weight):
            self.height = height
            self.weight = weight
    
        def jiao(self):
            print('jiao')
    
    class People(Animal):
        def __init__(self,height,weight,name,age):
            Animal.__init__(self,height,weight)  #這個其實就是函數調用,不要繼承也能實現
            self.name = name
            self.age = age
    
        def read(self):
            print('read')

派生方法二

  1. super().__init__方法

    class Animal:
        def __init__(self,height,weight):
            self.height = height
            self.weight = weight
    
        def jiao(self):
            print('jiao')
    
    class People(Animal):
        def __init__(self,height,weight,name,age):
            super().__init__(self,height,weight)  # python3中裏面不須要添加參數,可是python2 中須要添加super(People,self)
            self.name = name
            self.age = age
    
        def read(self):
            print('read')

類的組合

組合是用來解決類與類之間代碼冗餘的問題

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

class Students(People):
    def __init__(self,name,gender,id):
        super().__init__(name,gender)
        self.id = id

    def choose_course(self,course):
        print(f'{self.name}選擇了{course.name}')


class Teachers(People):
    def __init__(self,name,gender,level):
        super().__init__(name,gender)
        self.level = level

    def score(self, student, course, score):
        print(f'{self.name}給學生{student.name}課程{course.name}打分{score}')


class Course():
    def __init__(self,name,price):
        self.name =name
        self.price = price

class Admin(People):
    def create_course(self,name,price):  # create_course = Course
        course = Course(name,price)
        print(f'{self.name}建立了課程{name}')
        return course


# 實例化學生
rayn = Students('rayn','male','01')
rose = Students('rose','fmale','02')

# 實例化老師
nick = Teachers('nick','male',1)
tank = Teachers('tank','fmale',2)

#實例化管理員
egon = Admin('egon','male')

# 實例化課程
python = egon.create_course('python','20000') #egon.create_course == egon.Course
linux = egon.create_course('linux','18000')

# print(python.__dict__)

rayn.choose_course(python)
rose.choose_course(linux)

nick.score(rayn,python,100)
tank.score(rose,linux,1)

菱形繼承問題

類的分類

  1. 新式類

    繼承了object的類以及該類的子類,都是新式類;python3中全部的類都是新式類。

  2. 經典類

    沒有繼承object的類以及該類的子類,都是經典類,只有python2中才有經典類。

菱形繼承問題

若是繼承關係爲菱形結構,即子類的父類最後繼承了同一個類,那麼屬性的查找方式有兩種:

  1. 經典類:深度優先
  2. 新式類:廣度優先

咱們也能夠用print(類名.mro())來查看類的繼承關係

類的多態與多態性

多態:

多態值的是一類事物有多種形態(一個抽象類有多個子類,於是多態的概念依賴於繼承)

多態性:

多態性是指具備不一樣功能的函數,能夠使用相同的函數名;這樣就能夠用一個函數名調用不一樣內容的函數。

相關文章
相關標籤/搜索