Tùy chọn kế thừa nào được hỗ trợ trong python?

Lớp mới được tạo ra được gọi là lớp con [lớp con hoặc lớp dẫn xuất] và lớp hiện có mà lớp con được dẫn xuất từ ​​đó được gọi là lớp cha [lớp cha hoặc lớp cơ sở]

Cú pháp kế thừa Python

Đây là cú pháp kế thừa trong Python,

# define a superclass
class super_class:
    # attributes and method definition

# inheritance
class sub_class[super_class]:
    # attributes and method of super_class
    # attributes and method of sub_class

Ở đây, chúng ta đang kế thừa lớp

class Animal:

    # attribute and method of the parent class
    name = ""
    
    def eat[self]:
        print["I can eat"]

# inherit from Animal
class Dog[Animal]:

    # new method in subclass
    def display[self]:
        # access name attribute of superclass using self
        print["My name is ", self.name]

# create an object of the subclass
labrador = Dog[]

# access superclass attribute and method 
labrador.name = "Rohu"
labrador.eat[]

# call subclass method 
labrador.display[]
0 từ lớp
class Animal:

    # attribute and method of the parent class
    name = ""
    
    def eat[self]:
        print["I can eat"]

# inherit from Animal
class Dog[Animal]:

    # new method in subclass
    def display[self]:
        # access name attribute of superclass using self
        print["My name is ", self.name]

# create an object of the subclass
labrador = Dog[]

# access superclass attribute and method 
labrador.name = "Rohu"
labrador.eat[]

# call subclass method 
labrador.display[]
1

Ví dụ. Kế thừa Python

class Animal:

    # attribute and method of the parent class
    name = ""
    
    def eat[self]:
        print["I can eat"]

# inherit from Animal
class Dog[Animal]:

    # new method in subclass
    def display[self]:
        # access name attribute of superclass using self
        print["My name is ", self.name]

# create an object of the subclass
labrador = Dog[]

# access superclass attribute and method 
labrador.name = "Rohu"
labrador.eat[]

# call subclass method 
labrador.display[]

đầu ra

I can eat
My name is  Rohu

Trong ví dụ trên, chúng ta đã dẫn xuất một phân lớp Dog từ một siêu lớp Animal. Lưu ý các tuyên bố,

labrador.name = "Rohu"

labrador.eat[]

Ở đây, chúng tôi đang sử dụng labrador [đối tượng của Chó] để truy cập tên và

class Animal:

    # attribute and method of the parent class
    name = ""
    
    def eat[self]:
        print["I can eat"]

# inherit from Animal
class Dog[Animal]:

    # new method in subclass
    def display[self]:
        # access name attribute of superclass using self
        print["My name is ", self.name]

# create an object of the subclass
labrador = Dog[]

# access superclass attribute and method 
labrador.name = "Rohu"
labrador.eat[]

# call subclass method 
labrador.display[]
2 của lớp Động vật. Điều này là có thể bởi vì lớp con kế thừa tất cả các thuộc tính và phương thức của lớp cha

Ngoài ra, chúng ta đã truy cập thuộc tính name bên trong phương thức của lớp Dog bằng cách sử dụng

class Animal:

    # attribute and method of the parent class
    name = ""
    
    def eat[self]:
        print["I can eat"]

# inherit from Animal
class Dog[Animal]:

    # new method in subclass
    def display[self]:
        # access name attribute of superclass using self
        print["My name is ", self.name]

# create an object of the subclass
labrador = Dog[]

# access superclass attribute and method 
labrador.name = "Rohu"
labrador.eat[]

# call subclass method 
labrador.display[]
3

là một mối quan hệ

Trong Python, thừa kế là một mối quan hệ is-a. Nghĩa là, chúng ta chỉ sử dụng tính kế thừa nếu tồn tại mối quan hệ is-a giữa hai lớp. Ví dụ,

  1. Xe hơi là phương tiện
  2. Táo là một loại trái cây
  3. Mèo là một con vật

Ở đây, Xe có thể kế thừa từ Xe, Apple có thể kế thừa từ Trái cây, v.v.

Ghi đè phương thức trong Kế thừa Python

Trong ví dụ trước, chúng ta thấy đối tượng của lớp con có thể truy cập phương thức của lớp cha

Tuy nhiên, nếu cùng một phương thức xuất hiện trong cả lớp cha và lớp con thì sao?

Trong trường hợp này, phương thức trong lớp con sẽ ghi đè phương thức trong lớp cha. Khái niệm này được gọi là ghi đè phương thức trong Python

Ví dụ. Ghi đè phương thức

________số 8

đầu ra

I like to eat bones

Trong ví dụ trên, cùng một phương thức

class Animal:

    # attribute and method of the parent class
    name = ""
    
    def eat[self]:
        print["I can eat"]

# inherit from Animal
class Dog[Animal]:

    # new method in subclass
    def display[self]:
        # access name attribute of superclass using self
        print["My name is ", self.name]

# create an object of the subclass
labrador = Dog[]

# access superclass attribute and method 
labrador.name = "Rohu"
labrador.eat[]

# call subclass method 
labrador.display[]
2 có mặt trong cả lớp Chó và lớp Động vật

Bây giờ, khi chúng ta gọi phương thức

class Animal:

    # attribute and method of the parent class
    name = ""
    
    def eat[self]:
        print["I can eat"]

# inherit from Animal
class Dog[Animal]:

    # new method in subclass
    def display[self]:
        # access name attribute of superclass using self
        print["My name is ", self.name]

# create an object of the subclass
labrador = Dog[]

# access superclass attribute and method 
labrador.name = "Rohu"
labrador.eat[]

# call subclass method 
labrador.display[]
2 bằng cách sử dụng đối tượng của lớp con Dog, phương thức của lớp Dog được gọi

Điều này là do phương thức

class Animal:

    # attribute and method of the parent class
    name = ""
    
    def eat[self]:
        print["I can eat"]

# inherit from Animal
class Dog[Animal]:

    # new method in subclass
    def display[self]:
        # access name attribute of superclass using self
        print["My name is ", self.name]

# create an object of the subclass
labrador = Dog[]

# access superclass attribute and method 
labrador.name = "Rohu"
labrador.eat[]

# call subclass method 
labrador.display[]
2 của lớp con Dog ghi đè phương thức tương tự của lớp cha Animal

Phương thức super[] trong Kế thừa Python

Trước đây chúng ta đã thấy rằng cùng một phương thức trong lớp con sẽ ghi đè phương thức trong lớp cha

Tuy nhiên, nếu chúng ta cần truy cập phương thức của lớp cha từ lớp con, chúng ta sử dụng phương thức

class Animal:

    # attribute and method of the parent class
    name = ""
    
    def eat[self]:
        print["I can eat"]

# inherit from Animal
class Dog[Animal]:

    # new method in subclass
    def display[self]:
        # access name attribute of superclass using self
        print["My name is ", self.name]

# create an object of the subclass
labrador = Dog[]

# access superclass attribute and method 
labrador.name = "Rohu"
labrador.eat[]

# call subclass method 
labrador.display[]
7. Ví dụ,

class Animal:

    # attribute and method of the parent class
    name = ""
    
    def eat[self]:
        print["I can eat"]

# inherit from Animal
class Dog[Animal]:

    # new method in subclass
    def display[self]:
        # access name attribute of superclass using self
        print["My name is ", self.name]

# create an object of the subclass
labrador = Dog[]

# access superclass attribute and method 
labrador.name = "Rohu"
labrador.eat[]

# call subclass method 
labrador.display[]
4

đầu ra

class Animal:

    # attribute and method of the parent class
    name = ""
    
    def eat[self]:
        print["I can eat"]

# inherit from Animal
class Dog[Animal]:

    # new method in subclass
    def display[self]:
        # access name attribute of superclass using self
        print["My name is ", self.name]

# create an object of the subclass
labrador = Dog[]

# access superclass attribute and method 
labrador.name = "Rohu"
labrador.eat[]

# call subclass method 
labrador.display[]
5

Trong ví dụ trên, phương thức

class Animal:

    # attribute and method of the parent class
    name = ""
    
    def eat[self]:
        print["I can eat"]

# inherit from Animal
class Dog[Animal]:

    # new method in subclass
    def display[self]:
        # access name attribute of superclass using self
        print["My name is ", self.name]

# create an object of the subclass
labrador = Dog[]

# access superclass attribute and method 
labrador.name = "Rohu"
labrador.eat[]

# call subclass method 
labrador.display[]
2 của lớp con Dog sẽ ghi đè phương thức tương tự của lớp cha Animal

Python có hỗ trợ tất cả các loại kế thừa không?

Kế thừa là một tính năng bắt buộc của mọi ngôn ngữ lập trình hướng đối tượng. Điều này có nghĩa là Python hỗ trợ tính kế thừa và như bạn sẽ thấy ở phần sau, đây là một trong số ít ngôn ngữ hỗ trợ tính đa kế thừa.

Kế thừa cho Python là gì?

Tính kế thừa cho phép chúng ta định nghĩa một lớp kế thừa tất cả các phương thức và thuộc tính từ một lớp khác . Lớp cha là lớp được kế thừa từ đó, còn được gọi là lớp cơ sở. Lớp con là lớp kế thừa từ lớp khác, còn gọi là lớp dẫn xuất.

Python có hỗ trợ kế thừa lai không?

Bây giờ, chúng tôi xác định Lớp con có tên là “Sinh viên”, gồm các lớp Khóa học và Chi nhánh, để tạo ra Kế thừa kiểu kết hợp này . Theo cách tương tự, chúng ta có thể định nghĩa lớp và gọi các thành viên khác nhau của lớp cha. Hàm tạo không tham số để khởi tạo các thành viên dữ liệu.

Python có hỗ trợ kế thừa đơn lẻ không?

Có bốn loại kế thừa có sẵn trong Python . Kế thừa đơn Kế thừa đơn cho phép một lớp dẫn xuất kế thừa các thuộc tính của một lớp cha và điều này cho phép sử dụng lại mã và giới thiệu các tính năng bổ sung trong mã hiện có.

Chủ Đề