Hướng dẫn example of overriding in python - ví dụ về ghi đè trong python

Xem thảo luận

Cải thiện bài viết

Lưu bài viết

  • Đọc
  • Bàn luận
  • Xem thảo luận

    Cải thiện bài viết

    Lưu bài viết

    Đọc Inheritance in Python

    Bàn luậnoverride the method in the super-class.

    Hướng dẫn example of overriding in python - ví dụ về ghi đè trong python

    Điều kiện tiên quyết: Di truyền trong Python

    Example:

    Phương pháp ghi đè là khả năng của bất kỳ ngôn ngữ lập trình hướng đối tượng nào cho phép lớp con hoặc lớp con cung cấp một triển khai cụ thể của một phương thức đã được cung cấp bởi một trong các lớp siêu hoặc lớp của nó. Khi một phương thức trong lớp con có cùng tên, cùng một tham số hoặc chữ ký và cùng loại trả về (hoặc loại con) làm phương thức trong siêu lớp của nó, thì phương thức trong lớp con được cho là ghi đè phương thức trong siêu lớp.

    Phiên bản của một phương thức được thực thi sẽ được xác định bởi đối tượng được sử dụng để gọi nó. Nếu một đối tượng của lớp cha được sử dụng để gọi phương thức, thì phiên bản trong lớp cha sẽ được thực thi, nhưng nếu một đối tượng của lớp con được sử dụng để gọi phương thức, thì phiên bản trong lớp con sẽ được thực thi. Nói cách khác, đó là loại đối tượng được đề cập (không phải là loại của biến tham chiếu) xác định phiên bản nào của phương thức ghi đè sẽ được thực thi.

    class Parent():

        def

    Inside Child
    Inside Parent2
    
    0
    Inside Child
    Inside Parent2
    
    1
    Inside Child
    Inside Parent2
    
    2

    Inside Child
    Inside Parent2
    
    3
    Inside GrandChild
    Inside Parent
    
    4
    Inside GrandChild
    Inside Parent
    
    5
    Inside Child
    Inside Parent2
    
    1
    Inside GrandChild
    Inside Parent
    
    7

    Inside Child
    Inside Parent2
    
    3
    Inside Child
    Inside Parent2
    
    1
    Inside Child
    Inside Parent2
    
    5
    Inside Child
    Inside Parent2
    
    6
    Inside Child
    Inside Parent2
    
    7

    Phiên bản của một phương thức được thực thi sẽ được xác định bởi đối tượng được sử dụng để gọi nó. Nếu một đối tượng của lớp cha được sử dụng để gọi phương thức, thì phiên bản trong lớp cha sẽ được thực thi, nhưng nếu một đối tượng của lớp con được sử dụng để gọi phương thức, thì phiên bản trong lớp con sẽ được thực thi. Nói cách khác, đó là loại đối tượng được đề cập (không phải là loại của biến tham chiếu) xác định phiên bản nào của phương thức ghi đè sẽ được thực thi.

    class Parent():

        def

    Inside Child
    Inside Parent2
    
    0
    Inside Child
    Inside Parent2
    
    1
    Inside Child
    Inside Parent2
    
    2

    Inside Child
    Inside Parent2
    
    3
    Inside GrandChild
    Inside Parent
    
    4
    Inside GrandChild
    Inside Parent
    
    5
    Inside Child
    Inside Parent2
    
    1
    Inside GrandChild
    Inside Parent
    
    7

    Inside Child
    Inside Parent2
    
    3
    Inside Child
    Inside Parent2
    
    1
    Inside Child
    Inside Parent2
    
    5
    Inside Child
    Inside Parent2
    
    6
    Inside Child
    Inside Parent2
    
    7

        def

    Inside GrandChild
    Inside Parent
    
    0
    Inside Child
    Inside Parent2
    
    1
    Inside Child
    Inside Parent2
    
    2

    HEY !!!!!! GfG I am initialised(Class GEG3)
    HEY !!!!!! GfG I am initialised(Class GEG2)
    HEY !!!!!! GfG I am initialised(Class GEG1)
    Printing from class GFG3: 10
    Printing from class GFG2: 11
    Printing from class GFG1: 12
    
    6

    HEY !!!!!! GfG I am initialised(Class GEG3)
    HEY !!!!!! GfG I am initialised(Class GEG2)
    HEY !!!!!! GfG I am initialised(Class GEG1)
    Printing from class GFG3: 10
    Printing from class GFG2: 11
    Printing from class GFG1: 12
    
    7

    Output:

    Inside Parent
    Inside Child
    

    class
    Inside GrandChild
    Inside Parent
    
    9

    1. Inside Child
      Inside Parent2
      
      3
      Inside Child
      Inside Parent2
      
      1
      Inside Child
      Inside Parent2
      
      5
      Inside Child
      Inside Parent2
      
      6
      Inside Parent
      Inside Child
      
      9
      When a class is derived from more than one base class it is called multiple Inheritance.

      HEY !!!!!! GfG I am initialised(Class GEG3)
      HEY !!!!!! GfG I am initialised(Class GEG2)
      HEY !!!!!! GfG I am initialised(Class GEG1)
      Printing from class GFG3: 10
      Printing from class GFG2: 11
      Printing from class GFG1: 12
      
      0
      Inside Child
      Inside Parent2
      
      6
      HEY !!!!!! GfG I am initialised(Class GEG3)
      HEY !!!!!! GfG I am initialised(Class GEG2)
      HEY !!!!!! GfG I am initialised(Class GEG1)
      Printing from class GFG3: 10
      Printing from class GFG2: 11
      Printing from class GFG1: 12
      
      2
      Let’s consider an example where we want to override a method of one parent class only. Below is the implementation.

      HEY !!!!!! GfG I am initialised(Class GEG3)
      HEY !!!!!! GfG I am initialised(Class GEG2)
      HEY !!!!!! GfG I am initialised(Class GEG1)
      Printing from class GFG3: 10
      Printing from class GFG2: 11
      Printing from class GFG1: 12
      
      3
      Inside Child
      Inside Parent2
      
      6
      HEY !!!!!! GfG I am initialised(Class GEG3)
      HEY !!!!!! GfG I am initialised(Class GEG2)
      HEY !!!!!! GfG I am initialised(Class GEG1)
      Printing from class GFG3: 10
      Printing from class GFG2: 11
      Printing from class GFG1: 12
      
      5

          def

      Inside Child
      Inside Parent2
      
      0
      Inside Child
      Inside Parent2
      
      1
      Inside Child
      Inside Parent2
      
      2

      Inside Child
      Inside Parent2
      
      3
      Inside GrandChild
      Inside Parent
      
      4
      Inside GrandChild
      Inside Parent
      
      5class8class9

      Inside Child
      Inside Parent2
      
      3
      Inside Child
      Inside Parent2
      
      1
      Inside Child
      Inside Parent2
      
      5
      Inside Child
      Inside Parent2
      
      6
      Inside Child
      Inside Parent2
      
      7

          def

      Inside GrandChild
      Inside Parent
      
      0
      Inside Child
      Inside Parent2
      
      1
      Inside Child
      Inside Parent2
      
      2

      Inside Child
      Inside Parent2
      
      3
      Inside GrandChild
      Inside Parent
      
      4
      Inside GrandChild
      Inside Parent
      
      5    0class9

      class

      Inside GrandChild
      Inside Parent
      
      9

          def

      Inside Child
      Inside Parent2
      
      0
      Inside Child
      Inside Parent2
      
      1
      Inside Child
      Inside Parent2
      
      2

      Inside Child
      Inside Parent2
      
      3
      Inside GrandChild
      Inside Parent
      
      4
      Inside GrandChild
      Inside Parent
      
      5
      Inside Parent
      Inside Child
      
      9class9

      Inside Child
      Inside Parent2
      
      3
      Inside Child
      Inside Parent2
      
      1
      Inside Child
      Inside Parent2
      
      5
      Inside Child
      Inside Parent2
      
      6
      Inside Child
      Inside Parent2
      
      7

      def7

      def8

      Output:

      Inside Child
      Inside Parent2
      
    2.     def
      Inside GrandChild
      Inside Parent
      
      0
      Inside Child
      Inside Parent2
      
      1
      Inside Child
      Inside Parent2
      
      2
      When we have a child and grandchild relationship.

      class

      Inside GrandChild
      Inside Parent
      
      9 Let’s consider an example where we want to override only one method of one of its parent classes. Below is the implementation.

      Inside Child
      Inside Parent2
      
      3
      Inside Child
      Inside Parent2
      
      1
      Inside Child
      Inside Parent2
      
      5
      Inside Child
      Inside Parent2
      
      6
      Inside Parent
      Inside Child
      
      9

          def

      Inside GrandChild
      Inside Parent
      
      0
      Inside Child
      Inside Parent2
      
      1
      Inside Child
      Inside Parent2
      
      2

      Inside Child
      Inside Parent2
      
      3
      Inside GrandChild
      Inside Parent
      
      4
      Inside GrandChild
      Inside Parent
      
      5
      Inside Child
      Inside Parent2
      
      7class9

      class

      Inside GrandChild
      Inside Parent
      
      9

          def

      Inside Child
      Inside Parent2
      
      0
      Inside Child
      Inside Parent2
      
      1
      Inside Child
      Inside Parent2
      
      2

      Inside Child
      Inside Parent2
      
      3
      Inside GrandChild
      Inside Parent
      
      4
      Inside GrandChild
      Inside Parent
      
      5
      Inside Parent
      Inside Child
      
      9class9

      Inside Child
      Inside Parent2
      
      3
      Inside Child
      Inside Parent2
      
      1
      Inside Child
      Inside Parent2
      
      5
      Inside Child
      Inside Parent2
      
      6
      Inside Child
      Inside Parent2
      
      7

          def

      Inside Child
      Inside Parent2
      
      0
      Inside Child
      Inside Parent2
      
      1
      Inside Child
      Inside Parent2
      
      2

      Inside Child
      Inside Parent2
      
      3
      Inside GrandChild
      Inside Parent
      
      4
      Inside GrandChild
      Inside Parent
      
      5
      Inside Child
      Inside Parent2
      
      33
      Inside Child
      Inside Parent2
      
      34

      Inside Child
      Inside Parent2
      
      3
      Inside Child
      Inside Parent2
      
      1
      Inside Child
      Inside Parent2
      
      5
      Inside Child
      Inside Parent2
      
      6
      Inside Child
      Inside Parent2
      
      7

      Inside Child
      Inside Parent2
      
      38

      Inside Child
      Inside Parent2
      
      39

      Output:

      Inside GrandChild
      Inside Parent
      

        def
    Inside GrandChild
    Inside Parent
    
    0
    Inside Child
    Inside Parent2
    
    1
    Inside Child
    Inside Parent2
    
    2

    class

    Inside GrandChild
    Inside Parent
    
    9

    • Inside Child
      Inside Parent2
      
      3
      Inside Child
      Inside Parent2
      
      1
      Inside Child
      Inside Parent2
      
      5
      Inside Child
      Inside Parent2
      
      6
      Inside Parent
      Inside Child
      
      9
      Parent’s class methods can be called by using the Parent
      Inside Child
      Inside Parent2
      
      40 inside the overridden method.

      Example:

      Phương pháp ghi đè là khả năng của bất kỳ ngôn ngữ lập trình hướng đối tượng nào cho phép lớp con hoặc lớp con cung cấp một triển khai cụ thể của một phương thức đã được cung cấp bởi một trong các lớp siêu hoặc lớp của nó. Khi một phương thức trong lớp con có cùng tên, cùng một tham số hoặc chữ ký và cùng loại trả về (hoặc loại con) làm phương thức trong siêu lớp của nó, thì phương thức trong lớp con được cho là ghi đè phương thức trong siêu lớp.

          def

      Inside Child
      Inside Parent2
      
      0
      Inside Child
      Inside Parent2
      
      1
      Inside Child
      Inside Parent2
      
      2

      Inside Child
      Inside Parent2
      
      3
      Inside GrandChild
      Inside Parent
      
      4
      Inside GrandChild
      Inside Parent
      
      5
      Inside Child
      Inside Parent2
      
      7class9

      Inside Child
      Inside Parent2
      
      3
      Inside Child
      Inside Parent2
      
      1
      Inside Child
      Inside Parent2
      
      5
      Inside Child
      Inside Parent2
      
      6
      Inside Child
      Inside Parent2
      
      7

          def

      Inside Child
      Inside Parent2
      
      0
      Inside Child
      Inside Parent2
      
      1
      Inside Child
      Inside Parent2
      
      2

      Inside Child
      Inside Parent2
      
      3
      Inside Child
      Inside Parent2
      
      61
      Inside Child
      Inside Parent2
      
      1class9

      Inside Child
      Inside Parent2
      
      3
      Inside GrandChild
      Inside Parent
      
      4
      Inside GrandChild
      Inside Parent
      
      5
      Inside Parent
      Inside Child
      
      9class9

      Inside Child
      Inside Parent2
      
      3
      Inside Child
      Inside Parent2
      
      1
      Inside Child
      Inside Parent2
      
      5
      Inside Child
      Inside Parent2
      
      6
      Inside Child
      Inside Parent2
      
      7

      def7

      Output:

      Inside Parent
      Inside Child
      
    •     def
      Inside GrandChild
      Inside Parent
      
      0
      Inside Child
      Inside Parent2
      
      1
      Inside Child
      Inside Parent2
      
      2
      Python
      Inside Child
      Inside Parent2
      
      73 function provides us the facility to refer to the parent class explicitly. It is basically useful where we have to call superclass functions. It returns the proxy object that allows us to refer parent class by ‘super’.

      class

      Inside GrandChild
      Inside Parent
      
      9

      Phương pháp ghi đè là khả năng của bất kỳ ngôn ngữ lập trình hướng đối tượng nào cho phép lớp con hoặc lớp con cung cấp một triển khai cụ thể của một phương thức đã được cung cấp bởi một trong các lớp siêu hoặc lớp của nó. Khi một phương thức trong lớp con có cùng tên, cùng một tham số hoặc chữ ký và cùng loại trả về (hoặc loại con) làm phương thức trong siêu lớp của nó, thì phương thức trong lớp con được cho là ghi đè phương thức trong siêu lớp.

          def

      Inside Child
      Inside Parent2
      
      0
      Inside Child
      Inside Parent2
      
      1
      Inside Child
      Inside Parent2
      
      2

      Inside Child
      Inside Parent2
      
      3
      Inside GrandChild
      Inside Parent
      
      4
      Inside GrandChild
      Inside Parent
      
      5
      Inside Child
      Inside Parent2
      
      7class9

      Inside Child
      Inside Parent2
      
      3
      Inside Child
      Inside Parent2
      
      1
      Inside Child
      Inside Parent2
      
      5
      Inside Child
      Inside Parent2
      
      6
      Inside Child
      Inside Parent2
      
      7

          def

      Inside Child
      Inside Parent2
      
      0
      Inside Child
      Inside Parent2
      
      1
      Inside Child
      Inside Parent2
      
      2

      Inside Child
      Inside Parent2
      
      3
      Inside Child
      Inside Parent2
      
      94
      Inside Child
      Inside Parent2
      
      95

      Inside Child
      Inside Parent2
      
      3
      Inside GrandChild
      Inside Parent
      
      4
      Inside GrandChild
      Inside Parent
      
      5
      Inside Parent
      Inside Child
      
      9class9

      Inside Child
      Inside Parent2
      
      3
      Inside Child
      Inside Parent2
      
      1
      Inside Child
      Inside Parent2
      
      5
      Inside Child
      Inside Parent2
      
      6
      Inside Child
      Inside Parent2
      
      7

      def7

      Output:

      Inside Parent
      Inside Child
      

          def

      Inside GrandChild
      Inside Parent
      
      0
      Inside Child
      Inside Parent2
      
      1
      Inside Child
      Inside Parent2
      
      2

      class

      Inside GrandChild
      Inside Parent
      
      9

      Inside Child
      Inside Parent2
      
      3
      Inside Child
      Inside Parent2
      
      1
      Inside Child
      Inside Parent2
      
      5
      Inside Child
      Inside Parent2
      
      6
      Inside Parent
      Inside Child
      
      9

      Inside Child
      Inside Parent2
      
      3
      Inside GrandChild
      Inside Parent
      
      4
      Inside GrandChild
      Inside Parent
      
      5
      Inside GrandChild
      Inside Parent
      
      15
      Inside GrandChild
      Inside Parent
      
      16

      HEY !!!!!! GfG I am initialised(Class GEG3)
      HEY !!!!!! GfG I am initialised(Class GEG2)
      HEY !!!!!! GfG I am initialised(Class GEG1)
      Printing from class GFG3: 10
      Printing from class GFG2: 11
      Printing from class GFG1: 12
      
      0
      Inside Child
      Inside Parent2
      
      6
      HEY !!!!!! GfG I am initialised(Class GEG3)
      HEY !!!!!! GfG I am initialised(Class GEG2)
      HEY !!!!!! GfG I am initialised(Class GEG1)
      Printing from class GFG3: 10
      Printing from class GFG2: 11
      Printing from class GFG1: 12
      
      2

      Inside Child
      Inside Parent2
      
      3
      Inside GrandChild
      Inside Parent
      
      4
      Inside GrandChild
      Inside Parent
      
      5
      Inside GrandChild
      Inside Parent
      
      25
      Inside GrandChild
      Inside Parent
      
      26

      HEY !!!!!! GfG I am initialised(Class GEG3)
      HEY !!!!!! GfG I am initialised(Class GEG2)
      HEY !!!!!! GfG I am initialised(Class GEG1)
      Printing from class GFG3: 10
      Printing from class GFG2: 11
      Printing from class GFG1: 12
      
      3
      Inside Child
      Inside Parent2
      
      6
      HEY !!!!!! GfG I am initialised(Class GEG3)
      HEY !!!!!! GfG I am initialised(Class GEG2)
      HEY !!!!!! GfG I am initialised(Class GEG1)
      Printing from class GFG3: 10
      Printing from class GFG2: 11
      Printing from class GFG1: 12
      
      5

      Inside Child
      Inside Parent2
      
      3
      Inside Child
      Inside Parent2
      
      1
      Inside Child
      Inside Parent2
      
      5
      Inside Child
      Inside Parent2
      
      6
      Inside Parent
      Inside Child
      
      9

      Inside Child
      Inside Parent2
      
      3
      Inside GrandChild
      Inside Parent
      
      4
      Inside GrandChild
      Inside Parent
      
      5
      Inside GrandChild
      Inside Parent
      
      37
      Inside GrandChild
      Inside Parent
      
      16

      Inside Child
      Inside Parent2
      
      3
      Inside Child
      Inside Parent2
      
      94
      Inside GrandChild
      Inside Parent
      
      41

          def

      Inside GrandChild
      Inside Parent
      
      19
      Inside Child
      Inside Parent2
      
      1
      Inside GrandChild
      Inside Parent
      
      21

      Inside Child
      Inside Parent2
      
      3
      Inside GrandChild
      Inside Parent
      
      4
      Inside GrandChild
      Inside Parent
      
      5
      Inside GrandChild
      Inside Parent
      
      50
      Inside GrandChild
      Inside Parent
      
      26

      Inside Child
      Inside Parent2
      
      3
      Inside Child
      Inside Parent2
      
      94
      Inside GrandChild
      Inside Parent
      
      54
      Inside GrandChild
      Inside Parent
      
      55
      Inside GrandChild
      Inside Parent
      
      56
      Inside GrandChild
      Inside Parent
      
      16

      class

      Inside GrandChild
      Inside Parent
      
      59

          def

      Inside Child
      Inside Parent2
      
      0
      Inside Child
      Inside Parent2
      
      1
      Inside GrandChild
      Inside Parent
      
      11

      Inside Child
      Inside Parent2
      
      3
      Inside GrandChild
      Inside Parent
      
      4
      Inside GrandChild
      Inside Parent
      
      5
      Inside GrandChild
      Inside Parent
      
      68
      Inside GrandChild
      Inside Parent
      
      16

      Inside Child
      Inside Parent2
      
      3
      Inside Child
      Inside Parent2
      
      94
      Inside GrandChild
      Inside Parent
      
      41

          def

      Inside GrandChild
      Inside Parent
      
      19
      Inside Child
      Inside Parent2
      
      1
      Inside GrandChild
      Inside Parent
      
      21

      Inside Child
      Inside Parent2
      
      3
      Inside GrandChild
      Inside Parent
      
      4
      Inside GrandChild
      Inside Parent
      
      5
      Inside GrandChild
      Inside Parent
      
      81
      Inside GrandChild
      Inside Parent
      
      26

      Inside Child
      Inside Parent2
      
      3
      Inside Child
      Inside Parent2
      
      94
      Inside GrandChild
      Inside Parent
      
      54
      Inside GrandChild
      Inside Parent
      
      55
      Inside GrandChild
      Inside Parent
      
      56
      Inside GrandChild
      Inside Parent
      
      16

      class

      Inside GrandChild
      Inside Parent
      
      59

          def

      Inside Child
      Inside Parent2
      
      0
      Inside Child
      Inside Parent2
      
      1
      Inside GrandChild
      Inside Parent
      
      11

          

      Inside Parent
      Inside Child
      
      00
      Inside Parent
      Inside Child
      
      01class9

      Output:

      HEY !!!!!! GfG I am initialised(Class GEG3)
      HEY !!!!!! GfG I am initialised(Class GEG2)
      HEY !!!!!! GfG I am initialised(Class GEG1)
      Printing from class GFG3: 10
      Printing from class GFG2: 11
      Printing from class GFG1: 12
      

    Điều gì đang ghi đè trong Python với ví dụ?

    Phương thức ghi đè trong Python là khi bạn có hai phương thức có cùng tên mà mỗi người thực hiện các tác vụ khác nhau. Đây là một tính năng quan trọng của sự kế thừa trong Python. Trong phương pháp ghi đè, lớp con có thể thay đổi các chức năng của nó được xác định bởi các lớp tổ tiên của nó.when you have two methods with the same name that each perform different tasks. This is an important feature of inheritance in Python. In method overriding, the child class can change its functions that are defined by its ancestral classes.

    Điều gì đang vượt qua ví dụ?

    Phương pháp ghi đè ví dụ Chúng tôi có hai lớp: một cậu bé lớp trẻ và một con người lớp cha mẹ.Lớp cậu bé mở rộng lớp học của con người.Cả hai lớp có một phương pháp phổ biến void eat ().Boy Class đang đưa ra việc thực hiện riêng cho phương thức Eat () hay nói cách khác, nó đang ghi đè phương thức Eat ().Boy class is giving its own implementation to the eat() method or in other words it is overriding the eat() method.

    Có ghi đè trong Python không?

    Trong phương thức Python ghi đè xảy ra bằng cách xác định trong phương thức Lớp con A có cùng tên của một phương thức trong lớp cha.Khi bạn xác định một phương thức trong đối tượng, bạn thực hiện sau này có thể thỏa mãn cuộc gọi phương thức đó, vì vậy việc triển khai tổ tiên của nó không xuất hiện.. When you define a method in the object you make this latter able to satisfy that method call, so the implementations of its ancestors do not come in play.

    Tại sao chúng ta sử dụng ghi đè trong Python?

    Ghi đè phương thức là một khái niệm về lập trình định hướng đối tượng cho phép chúng tôi thay đổi việc thực hiện một hàm trong lớp con được xác định trong lớp cha.Đó là khả năng của một lớp trẻ để thay đổi việc thực hiện bất kỳ phương pháp nào đã được cung cấp bởi một trong các lớp cha mẹ của nó (tổ tiên).