Thừa kế giải thích với một ví dụ trong python là gì?

Quá trình kế thừa các thuộc tính của lớp cha vào lớp con được gọi là kế thừa. Lớp hiện có được gọi là lớp cơ sở hoặc lớp cha và lớp mới được gọi là lớp con hoặc lớp con hoặc lớp dẫn xuất

Trong bài học Python này, bạn sẽ học về kế thừa, nạp chồng phương thức, ghi đè phương thức, các kiểu kế thừa và MRO (Thứ tự giải quyết phương thức)

Trong lập trình hướng đối tượng, tính kế thừa là một khía cạnh quan trọng. Mục đích chính của kế thừa là khả năng sử dụng lại mã bởi vì chúng ta có thể sử dụng lớp hiện có để tạo một lớp mới thay vì tạo nó từ đầu

Trong kế thừa, lớp con có được tất cả các thành viên dữ liệu, thuộc tính và chức năng từ lớp cha. Ngoài ra, một lớp con cũng có thể cung cấp cách triển khai cụ thể của nó cho các phương thức của lớp cha

Ví dụ: Trong thế giới thực, Xe hơi là một lớp con của lớp Phương tiện. Chúng ta có thể tạo Xe bằng cách kế thừa các thuộc tính của Xe như Bánh xe, Màu sắc, Bình xăng, động cơ và thêm các thuộc tính phụ vào Xe theo yêu cầu

cú pháp

class BaseClass:
  Body of base class
class DerivedClass(BaseClass):
  Body of derived class

Cũng thấy

  • Bài tập Python OOP

Mục lục

Các loại thừa kế

Trong Python, dựa trên số lớp con và lớp cha tham gia, có 5 kiểu kế thừa. Loại thừa kế được liệt kê dưới đây

  1. thừa kế duy nhất
  2. Đa thừa kế
  3. kế thừa đa cấp
  4. Kế thừa phân cấp
  5. Kế thừa lai

Bây giờ hãy xem từng chi tiết với một ví dụ

Thừa kế đơn

Trong kế thừa đơn, một lớp con kế thừa từ một lớp cha đơn. Đây là một lớp con và một lớp cha

Thừa kế giải thích với một ví dụ trong python là gì?
Thừa kế giải thích với một ví dụ trong python là gì?
Kế thừa đơn Python

Ví dụ

Hãy tạo một lớp cha có tên là

# Base class
class Vehicle:
    def Vehicle_info(self):
        print('Inside Vehicle class')

# Child class
class Car(Vehicle):
    def car_info(self):
        print('Inside Car class')

# Create object of Car
car = Car()

# access Vehicle's info using car object
car.Vehicle_info()
car.car_info()
7 và một lớp con có tên là
# Base class
class Vehicle:
    def Vehicle_info(self):
        print('Inside Vehicle class')

# Child class
class Car(Vehicle):
    def car_info(self):
        print('Inside Car class')

# Create object of Car
car = Car()

# access Vehicle's info using car object
car.Vehicle_info()
car.car_info()
8 để thực hiện kế thừa đơn

# Base class
class Vehicle:
    def Vehicle_info(self):
        print('Inside Vehicle class')

# Child class
class Car(Vehicle):
    def car_info(self):
        print('Inside Car class')

# Create object of Car
car = Car()

# access Vehicle's info using car object
car.Vehicle_info()
car.car_info()

đầu ra

Inside Vehicle class
Inside Car class

Đa thừa kế

Trong đa kế thừa, một lớp con có thể kế thừa từ nhiều lớp cha. Vì vậy, đây là một lớp con và nhiều lớp cha

Thừa kế giải thích với một ví dụ trong python là gì?
Thừa kế giải thích với một ví dụ trong python là gì?
Python đa kế thừa

Ví dụ

# Parent class 1
class Person:
    def person_info(self, name, age):
        print('Inside Person class')
        print('Name:', name, 'Age:', age)

# Parent class 2
class Company:
    def company_info(self, company_name, location):
        print('Inside Company class')
        print('Name:', company_name, 'location:', location)

# Child class
class Employee(Person, Company):
    def Employee_info(self, salary, skill):
        print('Inside Employee class')
        print('Salary:', salary, 'Skill:', skill)

# Create object of Employee
emp = Employee()

# access data
emp.person_info('Jessa', 28)
emp.company_info('Google', 'Atlanta')
emp.Employee_info(12000, 'Machine Learning')

đầu ra

Inside Person class
Name: Jessa Age: 28

Inside Company class
Name: Google location: Atlanta

Inside Employee class
Salary: 12000 Skill: Machine Learning

Trong ví dụ trên, chúng ta tạo hai lớp cha lần lượt là

# Base class
class Vehicle:
    def Vehicle_info(self):
        print('Inside Vehicle class')

# Child class
class Car(Vehicle):
    def car_info(self):
        print('Inside Car class')

# Create object of Car
car = Car()

# access Vehicle's info using car object
car.Vehicle_info()
car.car_info()
9 và
Inside Vehicle class
Inside Car class
0. Sau đó, chúng tôi tạo một đứa trẻ được gọi là
Inside Vehicle class
Inside Car class
1 kế thừa từ các lớp Người và Công ty

kế thừa đa cấp

Trong kế thừa đa cấp, một lớp kế thừa từ một lớp con hoặc lớp dẫn xuất. Giả sử ba lớp A, B, C. A là lớp cha, B là lớp con của A, C là lớp con của B. Nói cách khác, chúng ta có thể nói một chuỗi các lớp được gọi là kế thừa đa cấp

Thừa kế giải thích với một ví dụ trong python là gì?
Thừa kế giải thích với một ví dụ trong python là gì?
Kế thừa đa cấp Python

Ví dụ

# Base class
class Vehicle:
    def Vehicle_info(self):
        print('Inside Vehicle class')

# Child class
class Car(Vehicle):
    def car_info(self):
        print('Inside Car class')

# Create object of Car
car = Car()

# access Vehicle's info using car object
car.Vehicle_info()
car.car_info()
0

đầu ra

# Base class
class Vehicle:
    def Vehicle_info(self):
        print('Inside Vehicle class')

# Child class
class Car(Vehicle):
    def car_info(self):
        print('Inside Car class')

# Create object of Car
car = Car()

# access Vehicle's info using car object
car.Vehicle_info()
car.car_info()
1

Trong ví dụ trên, chúng ta có thể thấy có ba lớp tên là

Inside Vehicle class
Inside Car class
2,
Inside Vehicle class
Inside Car class
3,
Inside Vehicle class
Inside Car class
4.
Inside Vehicle class
Inside Car class
2 là lớp cha,
Inside Vehicle class
Inside Car class
3 là lớp con của Xe,
Inside Vehicle class
Inside Car class
4 là lớp con của
Inside Vehicle class
Inside Car class
3. Vì vậy, chúng ta có thể thấy chuỗi các lớp

Kế thừa phân cấp

Trong kế thừa phân cấp, nhiều hơn một lớp con được bắt nguồn từ một lớp cha. Nói cách khác, chúng ta có thể nói một lớp cha và nhiều lớp con

Thừa kế giải thích với một ví dụ trong python là gì?
Thừa kế giải thích với một ví dụ trong python là gì?
Kế thừa phân cấp Python

Ví dụ

Hãy tạo 'Vehicle' làm lớp cha và hai lớp con 'Car' và 'Truck' làm lớp cha

# Base class
class Vehicle:
    def Vehicle_info(self):
        print('Inside Vehicle class')

# Child class
class Car(Vehicle):
    def car_info(self):
        print('Inside Car class')

# Create object of Car
car = Car()

# access Vehicle's info using car object
car.Vehicle_info()
car.car_info()
9

đầu ra

# Base class
class Vehicle:
    def Vehicle_info(self):
        print('Inside Vehicle class')

# Child class
class Car(Vehicle):
    def car_info(self):
        print('Inside Car class')

# Create object of Car
car = Car()

# access Vehicle's info using car object
car.Vehicle_info()
car.car_info()
0

Kế thừa lai

Khi thừa kế bao gồm nhiều loại hoặc kết hợp các thừa kế khác nhau được gọi là thừa kế lai

Thừa kế giải thích với một ví dụ trong python là gì?
Thừa kế giải thích với một ví dụ trong python là gì?
Kế thừa lai Python

Ví dụ

# Base class
class Vehicle:
    def Vehicle_info(self):
        print('Inside Vehicle class')

# Child class
class Car(Vehicle):
    def car_info(self):
        print('Inside Car class')

# Create object of Car
car = Car()

# access Vehicle's info using car object
car.Vehicle_info()
car.car_info()
1

Ghi chú. Trong ví dụ trên, tồn tại tính chất phân cấp và đa kế thừa. Ở đây chúng ta đã tạo, lớp cha

Inside Vehicle class
Inside Car class
2 và hai lớp con có tên là
Inside Vehicle class
Inside Car class
3 và
# Parent class 1
class Person:
    def person_info(self, name, age):
        print('Inside Person class')
        print('Name:', name, 'Age:', age)

# Parent class 2
class Company:
    def company_info(self, company_name, location):
        print('Inside Company class')
        print('Name:', company_name, 'location:', location)

# Child class
class Employee(Person, Company):
    def Employee_info(self, salary, skill):
        print('Inside Employee class')
        print('Salary:', salary, 'Skill:', skill)

# Create object of Employee
emp = Employee()

# access data
emp.person_info('Jessa', 28)
emp.company_info('Google', 'Atlanta')
emp.Employee_info(12000, 'Machine Learning')
1, đây là sự kế thừa phân cấp

Một cái khác là

Inside Vehicle class
Inside Car class
4 kế thừa từ hai lớp cha có tên là
Inside Vehicle class
Inside Car class
3 và
Inside Vehicle class
Inside Car class
2. Đây là đa thừa kế

Hàm # Parent class 1 class Person: def person_info(self, name, age): print('Inside Person class') print('Name:', name, 'Age:', age) # Parent class 2 class Company: def company_info(self, company_name, location): print('Inside Company class') print('Name:', company_name, 'location:', location) # Child class class Employee(Person, Company): def Employee_info(self, salary, skill): print('Inside Employee class') print('Salary:', salary, 'Skill:', skill) # Create object of Employee emp = Employee() # access data emp.person_info('Jessa', 28) emp.company_info('Google', 'Atlanta') emp.Employee_info(12000, 'Machine Learning') 5 của Python

Khi một lớp kế thừa tất cả các thuộc tính và hành vi từ lớp cha được gọi là kế thừa. Trong trường hợp như vậy, lớp kế thừa là lớp con và lớp sau là lớp cha

Ở lớp con, chúng ta có thể tham chiếu đến lớp cha bằng cách sử dụng hàm

# Parent class 1
class Person:
    def person_info(self, name, age):
        print('Inside Person class')
        print('Name:', name, 'Age:', age)

# Parent class 2
class Company:
    def company_info(self, company_name, location):
        print('Inside Company class')
        print('Name:', company_name, 'location:', location)

# Child class
class Employee(Person, Company):
    def Employee_info(self, salary, skill):
        print('Inside Employee class')
        print('Salary:', salary, 'Skill:', skill)

# Create object of Employee
emp = Employee()

# access data
emp.person_info('Jessa', 28)
emp.company_info('Google', 'Atlanta')
emp.Employee_info(12000, 'Machine Learning')
5. Hàm siêu trả về một đối tượng tạm thời của lớp cha cho phép chúng ta gọi một phương thức của lớp cha bên trong một phương thức của lớp con

Lợi ích của việc sử dụng hàm

# Parent class 1
class Person:
    def person_info(self, name, age):
        print('Inside Person class')
        print('Name:', name, 'Age:', age)

# Parent class 2
class Company:
    def company_info(self, company_name, location):
        print('Inside Company class')
        print('Name:', company_name, 'location:', location)

# Child class
class Employee(Person, Company):
    def Employee_info(self, salary, skill):
        print('Inside Employee class')
        print('Salary:', salary, 'Skill:', skill)

# Create object of Employee
emp = Employee()

# access data
emp.person_info('Jessa', 28)
emp.company_info('Google', 'Atlanta')
emp.Employee_info(12000, 'Machine Learning')
5

  1. Chúng tôi không bắt buộc phải nhớ hoặc chỉ định tên
    # Parent class 1
    class Person:
        def person_info(self, name, age):
            print('Inside Person class')
            print('Name:', name, 'Age:', age)
    
    # Parent class 2
    class Company:
        def company_info(self, company_name, location):
            print('Inside Company class')
            print('Name:', company_name, 'location:', location)
    
    # Child class
    class Employee(Person, Company):
        def Employee_info(self, salary, skill):
            print('Inside Employee class')
            print('Salary:', salary, 'Skill:', skill)
    
    # Create object of Employee
    emp = Employee()
    
    # access data
    emp.person_info('Jessa', 28)
    emp.company_info('Google', 'Atlanta')
    emp.Employee_info(12000, 'Machine Learning')
    
    8 gốc để truy cập các phương thức của nó
  2. Chúng ta có thể sử dụng hàm
    # Parent class 1
    class Person:
        def person_info(self, name, age):
            print('Inside Person class')
            print('Name:', name, 'Age:', age)
    
    # Parent class 2
    class Company:
        def company_info(self, company_name, location):
            print('Inside Company class')
            print('Name:', company_name, 'location:', location)
    
    # Child class
    class Employee(Person, Company):
        def Employee_info(self, salary, skill):
            print('Inside Employee class')
            print('Salary:', salary, 'Skill:', skill)
    
    # Create object of Employee
    emp = Employee()
    
    # access data
    emp.person_info('Jessa', 28)
    emp.company_info('Google', 'Atlanta')
    emp.Employee_info(12000, 'Machine Learning')
    
    5 trong cả kế thừa đơn và đa thừa kế
  3. Hàm
    # Parent class 1
    class Person:
        def person_info(self, name, age):
            print('Inside Person class')
            print('Name:', name, 'Age:', age)
    
    # Parent class 2
    class Company:
        def company_info(self, company_name, location):
            print('Inside Company class')
            print('Name:', company_name, 'location:', location)
    
    # Child class
    class Employee(Person, Company):
        def Employee_info(self, salary, skill):
            print('Inside Employee class')
            print('Salary:', salary, 'Skill:', skill)
    
    # Create object of Employee
    emp = Employee()
    
    # access data
    emp.person_info('Jessa', 28)
    emp.company_info('Google', 'Atlanta')
    emp.Employee_info(12000, 'Machine Learning')
    
    5 hỗ trợ khả năng sử dụng lại mã vì không cần phải viết toàn bộ hàm

Ví dụ

# Base class
class Vehicle:
    def Vehicle_info(self):
        print('Inside Vehicle class')

# Child class
class Car(Vehicle):
    def car_info(self):
        print('Inside Car class')

# Create object of Car
car = Car()

# access Vehicle's info using car object
car.Vehicle_info()
car.car_info()
0

đầu ra

# Base class
class Vehicle:
    def Vehicle_info(self):
        print('Inside Vehicle class')

# Child class
class Car(Vehicle):
    def car_info(self):
        print('Inside Car class')

# Create object of Car
car = Car()

# access Vehicle's info using car object
car.Vehicle_info()
car.car_info()
1

Trong ví dụ trên, chúng ta tạo một lớp cha

Inside Vehicle class
Inside Car class
0 và lớp con
Inside Vehicle class
Inside Car class
1. Trong lớp
Inside Vehicle class
Inside Car class
1, chúng ta gọi phương thức của lớp cha bằng cách sử dụng hàm
# Parent class 1
class Person:
    def person_info(self, name, age):
        print('Inside Person class')
        print('Name:', name, 'Age:', age)

# Parent class 2
class Company:
    def company_info(self, company_name, location):
        print('Inside Company class')
        print('Name:', company_name, 'location:', location)

# Child class
class Employee(Person, Company):
    def Employee_info(self, salary, skill):
        print('Inside Employee class')
        print('Salary:', salary, 'Skill:', skill)

# Create object of Employee
emp = Employee()

# access data
emp.person_info('Jessa', 28)
emp.company_info('Google', 'Atlanta')
emp.Employee_info(12000, 'Machine Learning')
5

issubclass()

Trong Python, chúng ta có thể xác minh xem một lớp cụ thể có phải là lớp con của lớp khác hay không. Với mục đích này, chúng ta có thể sử dụng hàm tích hợp Python

Inside Person class
Name: Jessa Age: 28

Inside Company class
Name: Google location: Atlanta

Inside Employee class
Salary: 12000 Skill: Machine Learning
5. Hàm này trả về
Inside Person class
Name: Jessa Age: 28

Inside Company class
Name: Google location: Atlanta

Inside Employee class
Salary: 12000 Skill: Machine Learning
6 nếu lớp đã cho là lớp con của lớp đã chỉ định. Nếu không, nó sẽ trả về
Inside Person class
Name: Jessa Age: 28

Inside Company class
Name: Google location: Atlanta

Inside Employee class
Salary: 12000 Skill: Machine Learning
7

cú pháp

# Base class
class Vehicle:
    def Vehicle_info(self):
        print('Inside Vehicle class')

# Child class
class Car(Vehicle):
    def car_info(self):
        print('Inside Car class')

# Create object of Car
car = Car()

# access Vehicle's info using car object
car.Vehicle_info()
car.car_info()
2

Ở đâu,

  • # Parent class 1
    class Person:
        def person_info(self, name, age):
            print('Inside Person class')
            print('Name:', name, 'Age:', age)
    
    # Parent class 2
    class Company:
        def company_info(self, company_name, location):
            print('Inside Company class')
            print('Name:', company_name, 'location:', location)
    
    # Child class
    class Employee(Person, Company):
        def Employee_info(self, salary, skill):
            print('Inside Employee class')
            print('Salary:', salary, 'Skill:', skill)
    
    # Create object of Employee
    emp = Employee()
    
    # access data
    emp.person_info('Jessa', 28)
    emp.company_info('Google', 'Atlanta')
    emp.Employee_info(12000, 'Machine Learning')
    
    8. lớp cần kiểm tra
  • Inside Person class
    Name: Jessa Age: 28
    
    Inside Company class
    Name: Google location: Atlanta
    
    Inside Employee class
    Salary: 12000 Skill: Machine Learning
    9. một
    # Parent class 1
    class Person:
        def person_info(self, name, age):
            print('Inside Person class')
            print('Name:', name, 'Age:', age)
    
    # Parent class 2
    class Company:
        def company_info(self, company_name, location):
            print('Inside Company class')
            print('Name:', company_name, 'location:', location)
    
    # Child class
    class Employee(Person, Company):
        def Employee_info(self, salary, skill):
            print('Inside Employee class')
            print('Salary:', salary, 'Skill:', skill)
    
    # Create object of Employee
    emp = Employee()
    
    # access data
    emp.person_info('Jessa', 28)
    emp.company_info('Google', 'Atlanta')
    emp.Employee_info(12000, 'Machine Learning')
    
    8, loại hoặc một
    # Base class
    class Vehicle:
        def Vehicle_info(self):
            print('Inside Vehicle class')
    
    # Child class
    class Car(Vehicle):
        def car_info(self):
            print('Inside Car class')
    
    # Create object of Car
    car = Car()
    
    # access Vehicle's info using car object
    car.Vehicle_info()
    car.car_info()
    
    01 của các lớp hoặc kiểu dữ liệu

Ví dụ

# Base class
class Vehicle:
    def Vehicle_info(self):
        print('Inside Vehicle class')

# Child class
class Car(Vehicle):
    def car_info(self):
        print('Inside Car class')

# Create object of Car
car = Car()

# access Vehicle's info using car object
car.Vehicle_info()
car.car_info()
3

Ngoài ra, hãy xem Python isinstance()

Ghi đè phương thức

Trong kế thừa, tất cả các thành viên có sẵn trong lớp cha theo mặc định có sẵn trong lớp con. Nếu lớp con không thỏa mãn với việc triển khai của lớp cha, thì lớp con được phép định nghĩa lại phương thức đó bằng cách mở rộng các hàm bổ sung trong lớp con. Khái niệm này được gọi là ghi đè phương thức

Khi một phương thức của lớp con có cùng tên, cùng tham số và cùng kiểu trả về với phương thức trong lớp cha của nó, thì phương thức trong lớp con được cho là sẽ ghi đè lên phương thức trong lớp cha

Thừa kế giải thích với một ví dụ trong python là gì?
Thừa kế giải thích với một ví dụ trong python là gì?
Ghi đè phương thức Python

Ví dụ

# Base class
class Vehicle:
    def Vehicle_info(self):
        print('Inside Vehicle class')

# Child class
class Car(Vehicle):
    def car_info(self):
        print('Inside Car class')

# Create object of Car
car = Car()

# access Vehicle's info using car object
car.Vehicle_info()
car.car_info()
4

đầu ra

# Base class
class Vehicle:
    def Vehicle_info(self):
        print('Inside Vehicle class')

# Child class
class Car(Vehicle):
    def car_info(self):
        print('Inside Car class')

# Create object of Car
car = Car()

# access Vehicle's info using car object
car.Vehicle_info()
car.car_info()
5

Trong ví dụ trên, chúng ta tạo hai lớp có tên là

Inside Vehicle class
Inside Car class
2 (Lớp cha) và
Inside Vehicle class
Inside Car class
3 (Lớp con). Lớp Car mở rộng từ lớp Vehicle nên tất cả các thuộc tính của lớp cha đều có sẵn trong lớp con. Ngoài ra, lớp con đã định nghĩa lại phương thức
# Base class
class Vehicle:
    def Vehicle_info(self):
        print('Inside Vehicle class')

# Child class
class Car(Vehicle):
    def car_info(self):
        print('Inside Car class')

# Create object of Car
car = Car()

# access Vehicle's info using car object
car.Vehicle_info()
car.car_info()
04

Thứ tự giải quyết phương thức trong Python

Trong Python, Thứ tự phân giải phương thức (MRO) là thứ tự mà Python tìm kiếm một phương thức hoặc thuộc tính. Đầu tiên, phương thức hoặc thuộc tính được tìm kiếm trong một lớp, sau đó nó tuân theo thứ tự mà chúng ta đã chỉ định khi kế thừa

Thứ tự này còn được gọi là Tuyến tính hóa của một lớp và một bộ quy tắc được gọi là MRO (Thứ tự giải quyết phương pháp). MRO đóng một vai trò thiết yếu trong nhiều kế thừa vì một phương thức duy nhất có thể được tìm thấy trong nhiều lớp cha

Trong đa kế thừa, thứ tự tìm kiếm sau đây được tuân theo

  1. Đầu tiên, nó tìm kiếm trong lớp cha hiện tại nếu không có, sau đó tìm kiếm trong lớp cha được chỉ định trong khi kế thừa (nghĩa là từ trái sang phải. )
  2. Chúng ta có thể lấy MRO của một lớp. Với mục đích này, chúng ta có thể sử dụng thuộc tính
    # Base class
    class Vehicle:
        def Vehicle_info(self):
            print('Inside Vehicle class')
    
    # Child class
    class Car(Vehicle):
        def car_info(self):
            print('Inside Car class')
    
    # Create object of Car
    car = Car()
    
    # access Vehicle's info using car object
    car.Vehicle_info()
    car.car_info()
    
    05 hoặc phương thức
    # Base class
    class Vehicle:
        def Vehicle_info(self):
            print('Inside Vehicle class')
    
    # Child class
    class Car(Vehicle):
        def car_info(self):
            print('Inside Car class')
    
    # Create object of Car
    car = Car()
    
    # access Vehicle's info using car object
    car.Vehicle_info()
    car.car_info()
    
    06

Ví dụ

# Base class
class Vehicle:
    def Vehicle_info(self):
        print('Inside Vehicle class')

# Child class
class Car(Vehicle):
    def car_info(self):
        print('Inside Car class')

# Create object of Car
car = Car()

# access Vehicle's info using car object
car.Vehicle_info()
car.car_info()
6

Trong ví dụ trên, chúng tôi tạo ba lớp có tên là

# Base class
class Vehicle:
    def Vehicle_info(self):
        print('Inside Vehicle class')

# Child class
class Car(Vehicle):
    def car_info(self):
        print('Inside Car class')

# Create object of Car
car = Car()

# access Vehicle's info using car object
car.Vehicle_info()
car.car_info()
07,
# Base class
class Vehicle:
    def Vehicle_info(self):
        print('Inside Vehicle class')

# Child class
class Car(Vehicle):
    def car_info(self):
        print('Inside Car class')

# Create object of Car
car = Car()

# access Vehicle's info using car object
car.Vehicle_info()
car.car_info()
08 và
# Base class
class Vehicle:
    def Vehicle_info(self):
        print('Inside Vehicle class')

# Child class
class Car(Vehicle):
    def car_info(self):
        print('Inside Car class')

# Create object of Car
car = Car()

# access Vehicle's info using car object
car.Vehicle_info()
car.car_info()
09. Lớp
# Base class
class Vehicle:
    def Vehicle_info(self):
        print('Inside Vehicle class')

# Child class
class Car(Vehicle):
    def car_info(self):
        print('Inside Car class')

# Create object of Car
car = Car()

# access Vehicle's info using car object
car.Vehicle_info()
car.car_info()
08 kế thừa từ
# Base class
class Vehicle:
    def Vehicle_info(self):
        print('Inside Vehicle class')

# Child class
class Car(Vehicle):
    def car_info(self):
        print('Inside Car class')

# Create object of Car
car = Car()

# access Vehicle's info using car object
car.Vehicle_info()
car.car_info()
07, lớp
# Base class
class Vehicle:
    def Vehicle_info(self):
        print('Inside Vehicle class')

# Child class
class Car(Vehicle):
    def car_info(self):
        print('Inside Car class')

# Create object of Car
car = Car()

# access Vehicle's info using car object
car.Vehicle_info()
car.car_info()
09 kế thừa từ
# Base class
class Vehicle:
    def Vehicle_info(self):
        print('Inside Vehicle class')

# Child class
class Car(Vehicle):
    def car_info(self):
        print('Inside Car class')

# Create object of Car
car = Car()

# access Vehicle's info using car object
car.Vehicle_info()
car.car_info()
08 và
# Base class
class Vehicle:
    def Vehicle_info(self):
        print('Inside Vehicle class')

# Child class
class Car(Vehicle):
    def car_info(self):
        print('Inside Car class')

# Create object of Car
car = Car()

# access Vehicle's info using car object
car.Vehicle_info()
car.car_info()
07. Khi chúng ta tạo một đối tượng của lớp
# Base class
class Vehicle:
    def Vehicle_info(self):
        print('Inside Vehicle class')

# Child class
class Car(Vehicle):
    def car_info(self):
        print('Inside Car class')

# Create object of Car
car = Car()

# access Vehicle's info using car object
car.Vehicle_info()
car.car_info()
09 và gọi phương thức
# Base class
class Vehicle:
    def Vehicle_info(self):
        print('Inside Vehicle class')

# Child class
class Car(Vehicle):
    def car_info(self):
        print('Inside Car class')

# Create object of Car
car = Car()

# access Vehicle's info using car object
car.Vehicle_info()
car.car_info()
16, Python sẽ tìm kiếm phương thức
# Base class
class Vehicle:
    def Vehicle_info(self):
        print('Inside Vehicle class')

# Child class
class Car(Vehicle):
    def car_info(self):
        print('Inside Car class')

# Create object of Car
car = Car()

# access Vehicle's info using car object
car.Vehicle_info()
car.car_info()
16 trong lớp hiện tại trong chính lớp
# Base class
class Vehicle:
    def Vehicle_info(self):
        print('Inside Vehicle class')

# Child class
class Car(Vehicle):
    def car_info(self):
        print('Inside Car class')

# Create object of Car
car = Car()

# access Vehicle's info using car object
car.Vehicle_info()
car.car_info()
09

Sau đó tìm kiếm các lớp cha, cụ thể là

# Base class
class Vehicle:
    def Vehicle_info(self):
        print('Inside Vehicle class')

# Child class
class Car(Vehicle):
    def car_info(self):
        print('Inside Car class')

# Create object of Car
car = Car()

# access Vehicle's info using car object
car.Vehicle_info()
car.car_info()
08 và
# Base class
class Vehicle:
    def Vehicle_info(self):
        print('Inside Vehicle class')

# Child class
class Car(Vehicle):
    def car_info(self):
        print('Inside Car class')

# Create object of Car
car = Car()

# access Vehicle's info using car object
car.Vehicle_info()
car.car_info()
07, vì lớp
# Base class
class Vehicle:
    def Vehicle_info(self):
        print('Inside Vehicle class')

# Child class
class Car(Vehicle):
    def car_info(self):
        print('Inside Car class')

# Create object of Car
car = Car()

# access Vehicle's info using car object
car.Vehicle_info()
car.car_info()
09 kế thừa từ
# Base class
class Vehicle:
    def Vehicle_info(self):
        print('Inside Vehicle class')

# Child class
class Car(Vehicle):
    def car_info(self):
        print('Inside Car class')

# Create object of Car
car = Car()

# access Vehicle's info using car object
car.Vehicle_info()
car.car_info()
08 và
# Base class
class Vehicle:
    def Vehicle_info(self):
        print('Inside Vehicle class')

# Child class
class Car(Vehicle):
    def car_info(self):
        print('Inside Car class')

# Create object of Car
car = Car()

# access Vehicle's info using car object
car.Vehicle_info()
car.car_info()
07. nghĩa là,
# Base class
class Vehicle:
    def Vehicle_info(self):
        print('Inside Vehicle class')

# Child class
class Car(Vehicle):
    def car_info(self):
        print('Inside Car class')

# Create object of Car
car = Car()

# access Vehicle's info using car object
car.Vehicle_info()
car.car_info()
94 và luôn tìm kiếm theo cách từ trái sang phải