第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

全部開發(fā)者教程

Python 進(jìn)階應(yīng)用教程

Python 進(jìn)階應(yīng)用教程
01 Python 的對象和類 02 Python 類屬性和實(shí)例屬性 03 Python類的構(gòu)造方法、析構(gòu)方法、實(shí)例方法 04 Python 類的私有屬性和私有方法 05 Python 類的繼承和多繼承 06 Python 類實(shí)戰(zhàn) 07 Python 中的迭代器實(shí)現(xiàn)原理 08 Python 中的迭代器趣味實(shí)踐 09 Python 中的生成器實(shí)現(xiàn)原理 10 Python 中的生成器趣味實(shí)踐 11 Python 中的錯誤和異常 12 Python 中的異常處理 13 Python 中的模塊 14 Python 標(biāo)準(zhǔn)庫之 os 模塊 15 Python 標(biāo)準(zhǔn)庫之 sys 模塊 16 Python 標(biāo)準(zhǔn)庫之 math 模塊 17 Python 標(biāo)準(zhǔn)庫之 random 模塊 18 Python 標(biāo)準(zhǔn)庫之 Json 模塊 19 Python 標(biāo)準(zhǔn)庫 datetime 模塊 20 Python 中的常用第三方模塊 21 Python 中的命名空間 22 Python 中的作用域 23 Python 中的文件 IO 操作 24 Python 基礎(chǔ)實(shí)戰(zhàn) 25 Python 內(nèi)置函數(shù) 26 Python 中使用正則表達(dá)式 27 使用 Python 操作 MySQL 數(shù)據(jù)庫 28 使用 Python 操作 Mongo 數(shù)據(jù)庫 29 使用 Python 操作 Redis 數(shù)據(jù)庫 30 使用 Python 發(fā)送一封郵件 31 threading 之 Thread 的使用 32 threading 之 Lock 的基本使用 33 Python 生產(chǎn)者消費(fèi)者模型 34 Python 的內(nèi)存管理與垃圾回收 35 Python 領(lǐng)域運(yùn)用:網(wǎng)絡(luò)爬蟲 36 Python 領(lǐng)域運(yùn)用:Web 開發(fā) 37 Python 領(lǐng)域運(yùn)用:自動化運(yùn)維 38 Python 領(lǐng)域運(yùn)用:自動化測試

Python 類的繼承和多繼承

在面向?qū)ο蟮某绦蛟O(shè)計中,定義一個新的 class 的時候,可以從某個現(xiàn)有的 class 繼承,新的 class 稱為子類,而被繼承的 class 稱為基類、父類或超類。

Python 中繼承的語法如下:

class Parent:
    pass

class Child(Parent):
    pass
  • 在第 1 行,定義了父類 Parent;
  • 在第 4 行,定義了子類 Child,語法 Child(Parent) 表示類 Child 繼承于類 Parent。

子類繼承父類的屬性和方法,使得子類具有父類的屬性和方法,從而實(shí)現(xiàn)代碼重用;同時,子類可以增加自己特有的方法。例如,下圖中定義了 3 個類,類 Teacher 與類 Student 繼承于類 Person,如圖所示:

  • 父類 Person 定義了屬性 name 和 age,定義了方法 introduce,這些屬性和方法被子類繼承
  • 子類 Teacher 定義了自己特有的屬性 salary,定義了自己特有的方法 showSalary
  • 子類 Student 定義了自己特有的屬性 grade,定義了自己特有的方法 showGrade

1. 在子類中增加屬性和方法

1.1 編寫父類 Person

class Person:
    def __init__(self, name, age):
        self.name = name
        self.age = age

    def introduce(self):
        print('My name is', self.name)
        print('My age is', self.age)
  • 在第 2 行,定義構(gòu)造方法 __init__,設(shè)置屬性 name 和 age
  • 在第 6 行,定義方法 introduce,打印屬性 name 和 age

1.2 編寫子類 Teacher

class Teacher(Person):
    def __init__(self, name, age, salary):
        self.name = name
        self.age = age
        self.salary = salary

    def showSalary(self):
        print('My salary is', self.salary)
  • 在第 1 行,通過語法 Teacher(Person),定義繼承于 Person 的類 Teacher
  • 在第 5 行,在構(gòu)造方法中,增加類 Teacher 特有的屬性 salary
  • 在第 7 行,定義方法 showSalary,打印屬性 salary

1.3 編寫子類 Student

class Student(Person):
    def __init__(self, name, age, grade):
        self.name = name
        self.age = age
        self.grade = grade

    def showGrade(self):
        print('My grade is', self.grade)
  • 在第 1 行,通過語法 Student(Person),定義繼承于 Person 的類 Student
  • 在第 5 行,在構(gòu)造方法中,增加類 Student 特有的屬性 grade
  • 在第 7 行,定義方法 showGrade,打印屬性 grade

1.4 創(chuàng)建實(shí)例

teacher = Teacher('tom', 30, 5000)
teacher.introduce()
teacher.showSalary()
print()
student = Student('jerry', 10, 90)
student.introduce()
student.showGrade()
  • 在第 1 行,創(chuàng)建實(shí)例 teacher
    • 在第 2 行,調(diào)用父類方法 introduce
    • 在第 3 行,調(diào)用自己特有的方法 showSalary
  • 在第 5 行,創(chuàng)建實(shí)例 student
    • 在第 6 行,調(diào)用父類方法 introduce
    • 在第 7 行,調(diào)用自己特有的方法 showGrade

運(yùn)行程序,輸出如下結(jié)果

My name is tom
My age is 30
My salary is 5000

My name is jerry
My age is 10
My grade is 90
  • 第 1 行到第 3 行,是 teacher 的輸出結(jié)果
  • 第 5 行到第 7 行,是 student 的輸出結(jié)果

2. 在子類中調(diào)用父類的構(gòu)造方法

2.1 代碼重復(fù)的問題

在前面的小節(jié)中,類 Person 的構(gòu)造方法如下:

class Person:
    def __init__(self, name, age):
        self.name = name
        self.age = age

類 Teacher 的構(gòu)造方法如下:

class Teacher(Person):
    def __init__(self, name, age, salary):
        self.name = name
        self.age = age
        self.salary = salary

類 Student 的構(gòu)造方法如下:

class Student(Person):
    def __init__(self, name, age, grade):
        self.name = name
        self.age = age
        self.grade = grade

在這 3 段初始化代碼中,存在明顯的代碼重復(fù),我們希望:

  • 初始化類 Teacher 的屬性 name、age、salary 時,可以重用父類 Person 的初始化代碼
  • 初始化類 Student 的屬性 name、age、grade 時,可以重用父類 Person 的初始化代碼

2.2 編寫父類 Person

class Person:
    def __init__(self, name, age):
        self.name = name
        self.age = age

    def introduce(self):
        print('My name is', self.name)
        print('My age is', self.age)
  • 在第 2 行,定義構(gòu)造方法 __init__,設(shè)置屬性 name 和 age
  • 在第 6 行,定義方法 introduce,打印屬性 name 和 age

2.3 編寫子類 Teacher

class Teacher(Person):
    def __init__(self, name, age, salary):
        Person.__init__(self, name, age)
        self.salary = salary

    def showSalary(self):
        print('My salary is', self.salary)
  • 在第 1 行,通過語法 Teacher(Person),定義繼承于 Person 的類 Teacher
  • 在第 3 行,通過語法 Person.__init(self, name, age)__ 調(diào)用父類的構(gòu)造方法 __init__,對屬性 name 和 age 進(jìn)行設(shè)置
  • 在第 4 行,在構(gòu)造方法中,增加類 Teacher 特有的屬性 salary
  • 在第 6 行,定義方法 showSalary,打印屬性 salary

2.4 編寫子類 Student

class Student(Person):
    def __init__(self, name, age, grade):
        Person.__init__(self, name, age)
        self.grade = grade

    def showGrade(self):
        print('My grade is', self.grade)
  • 在第 1 行,通過語法 Student(Person),定義繼承于 Person 的類 Student
  • 在第 3 行,通過語法 Person.__init(self, name, age)__ 調(diào)用父類的構(gòu)造方法 __init__,對屬性 name 和 age 進(jìn)行設(shè)置
  • 在第 4 行,在構(gòu)造方法中,增加類 Student 特有的屬性 grade
  • 在第 6 行,定義方法 showGrade,打印屬性 grade

2.5 創(chuàng)建實(shí)例

teacher = Teacher('tom', 30, 5000)
teacher.introduce()
teacher.showSalary()
print()
student = Student('jerry', 10, 90)
student.introduce()
student.showGrade()
  • 在第 1 行,創(chuàng)建實(shí)例 teacher
    • 在第 2 行,調(diào)用父類方法 introduce
    • 在第 3 行,調(diào)用自己特有的方法 showSalary
  • 在第 5 行,創(chuàng)建實(shí)例 student
    • 在第 6 行,調(diào)用父類方法 introduce
    • 在第 7 行,調(diào)用自己特有的方法 showGrade

運(yùn)行程序,輸出如下結(jié)果

My name is tom
My age is 30
My salary is 5000

My name is jerry
My age is 10
My grade is 90
  • 第 1 行到第 3 行,是 teacher 的輸出結(jié)果
  • 第 5 行到第 7 行,是 student 的輸出結(jié)果

3. 多繼承

定義一個新的 class 的時候,可以從多個現(xiàn)有的 class 繼承,如果繼承多個父類,稱為多繼承。Python 中多繼承的語法如下:

class Father:
    pass

class Mother:
    pass

class Child(Father, Mother):
    pass
  • 在第 1 行,定義了父類 Father
  • 在第 4 行,定義了父類 Mother
  • 在第 7 行,定義了子類 Child,它繼承于兩個父類:Father 和 Mother

子類繼承所有父類的屬性和方法,從而實(shí)現(xiàn)代碼重用。

4. 多繼承的例子

4.1 概述

本節(jié)構(gòu)造 3 個類:Father、Mother 和 Child,Child 繼承于兩個類 Father 和 Mother,它繼承了這兩個類的屬性和方法,同時有自己特有的屬性和方法,如下圖所示:

4.2 編寫父類 Father

class Father:
    def __init__(self, father_attr):
        self.father_attr = father_attr

    def father_method(self):
        print('father_attr =', self.father_attr)
  • 在第 3 行,定義類 Father 的屬性 father_attr
  • 在第 5 行,定義類 Father 的方法 father_method

4.3 編寫父類 Mother

class Mother:
    def __init__(self, mother_attr):
        self.mother_attr = mother_attr

    def mother_method(self):
        print('mother_attr =', self.mother_attr)
  • 在第 3 行,定義類 Mother 的屬性 mother_attr
  • 在第 5 行,定義類 Mother 的方法 mother_method

4.4 編寫子類 Child

class Child(Father, Mother):
    def __init__(self, father_attr, mother_attr, child_attr):
        Father.__init__(self, father_attr)
        Mother.__init__(self, mother_attr)
        self.child_attr = child_attr

    def child_method(self):
        print('child_attr =', self.child_attr)     
  • 在第 1 行,定義類 Child,繼承于 Father 和 Mother
  • 在第 3 行,調(diào)用父類 Father 的構(gòu)造方法
  • 在第 4 行,調(diào)用父類 Mother 的構(gòu)造方法
  • 在第 7 行,定義類 Child 的方法 child_method

4.5 創(chuàng)建實(shí)例

child = Child('Father', 'Mother', 'Child')        
child.father_method()
child.mother_method()
child.child_method()
  • 在第 1 行,創(chuàng)建實(shí)例 Child
  • 在第 2 行,調(diào)用繼承于父類 Father 的方法 father_method
  • 在第 3 行,調(diào)用繼承于父類 Mother 的方法 mother_method
  • 在第 4 行,調(diào)用類自己的方法 child_method

程序輸出結(jié)果如下:

father_attr = Father
mother_attr = Mother
child_attr = Child