Hướng dẫn constructor overloading in python geeksforgeeks - nạp chồng phương thức khởi tạo trong python geeksforgeeks

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 Object-Oriented Programming in Python, Object-Oriented Programming in Python | Set 2 
    Constructors are generally used for instantiating an object. The task of constructors is to initialize(assign values) to the data members of the class when an object of the class is created. In Python the __init__() method is called the constructor and is always called when an object is created.
    Syntax of constructor declaration : 

    def __init__(self):
        # body of the constructor

    Bàn luận 

    • Điều kiện tiên quyết: Lập trình hướng đối tượng trong Python, lập trình hướng đối tượng trong Python | Đặt 2 & nbsp; các hàm tạo thường được sử dụng để khởi tạo một đối tượng. Nhiệm vụ của các hàm tạo là khởi tạo (gán giá trị) cho các thành viên dữ liệu của lớp khi một đối tượng của lớp được tạo. Trong Python, phương thức __init __ () được gọi là hàm tạo và luôn được gọi khi một đối tượng được tạo.Syntax của khai báo hàm tạo: & nbsp; The default constructor is a simple constructor which doesn’t accept any arguments. Its definition has only one argument which is a reference to the instance being constructed.
    • Các loại nhà xây dựng: & nbsp; constructor with parameters is known as parameterized constructor. The parameterized constructor takes its first argument as a reference to the instance being constructed known as self and the rest of the arguments are provided by the programmer.

    Trình xây dựng mặc định: Trình xây dựng mặc định là một hàm tạo đơn giản mà không chấp nhận bất kỳ đối số nào. Định nghĩa của nó chỉ có một đối số là một tham chiếu đến trường hợp được xây dựng. 
     

    Python3

    hàm tạo tham số hóa: hàm tạo với các tham số được gọi là hàm tạo tham số hóa. Hàm tạo tham số hóa lấy đối số đầu tiên của nó làm tham chiếu đến thể hiện được xây dựng được gọi là bản thân và phần còn lại của các đối số được cung cấp bởi lập trình viên.

    Ví dụ về hàm tạo mặc định: & nbsp; & nbsp;

    class GeekforGeeks:

        def

    GeekforGeeks
    0
    GeekforGeeks
    1
    GeekforGeeks
    2

    GeekforGeeks
    3
    First number = 1000
    Second number = 2000
    Addition of two numbers = 3000
    4
    First number = 1000
    Second number = 2000
    Addition of two numbers = 3000
    5
    GeekforGeeks
    1
    First number = 1000
    Second number = 2000
    Addition of two numbers = 3000
    7

    GeekforGeeks
    3
    GeekforGeeks
    1
    GeekforGeeks
    5
    GeekforGeeks
    6
    GeekforGeeks
    7

    Sum of list : 15
    Square of int : 25
    String : Hello! GeeksforGeeks.
    
    1

        def

    First number = 1000
    Second number = 2000
    Addition of two numbers = 3000
    0
    GeekforGeeks
    1
    GeekforGeeks
    2
     

    GeekforGeeks

    First number = 1000
    Second number = 2000
    Addition of two numbers = 3000
    8
    GeekforGeeks
    6
    Sum of list : 15
    Square of int : 25
    String : Hello! GeeksforGeeks.
    
    0
    parameterized constructor : 

    Python3

    Đầu ra: & nbsp;

    Ví dụ về hàm tạo phân loại: & nbsp;

    class

    Sum of list : 15
    Square of int : 25
    String : Hello! GeeksforGeeks.
    
    3

        

    Sum of list : 15
    Square of int : 25
    String : Hello! GeeksforGeeks.
    
    5
    GeekforGeeks
    6
    Sum of list : 15
    Square of int : 25
    String : Hello! GeeksforGeeks.
    
    7

        

    Sum of list : 15
    Square of int : 25
    String : Hello! GeeksforGeeks.
    
    9
    GeekforGeeks
    6
    Sum of list : 15
    Square of int : 25
    String : Hello! GeeksforGeeks.
    
    7

        

    equation 2 : -3
    equation 1 : 2
    equation 3 : 11.0
    
    3
    GeekforGeeks
    6
    Sum of list : 15
    Square of int : 25
    String : Hello! GeeksforGeeks.
    
    7

        def

    GeekforGeeks
    0
    GeekforGeeks
    1
    equation 2 : -3
    equation 1 : 2
    equation 3 : 11.0
    
    0

    GeekforGeeks
    3
    GeekforGeeks
    1
    equation 2 : -3
    equation 1 : 2
    equation 3 : 11.0
    
    3
    GeekforGeeks
    6
    equation 2 : -3
    equation 1 : 2
    equation 3 : 11.0
    
    5

    GeekforGeeks
    3
    GeekforGeeks
    1____58
    GeekforGeeks
    6 class0

        def class3

    GeekforGeeks
    1
    GeekforGeeks
    2

    GeekforGeeks
    3
    First number = 1000
    Second number = 2000
    Addition of two numbers = 3000
    4
    First number = 1000
    Second number = 2000
    Addition of two numbers = 3000
    5class9 GeekforGeeks:0 GeekforGeeks:1
    First number = 1000
    Second number = 2000
    Addition of two numbers = 3000
    5
    GeekforGeeks
    1GeekforGeeks:4

    GeekforGeeks
    3
    First number = 1000
    Second number = 2000
    Addition of two numbers = 3000
    4
    First number = 1000
    Second number = 2000
    Addition of two numbers = 3000
    5GeekforGeeks:8 GeekforGeeks:0 GeekforGeeks:1
    First number = 1000
    Second number = 2000
    Addition of two numbers = 3000
    5
    GeekforGeeks
    1    3

    GeekforGeeks
    3
    First number = 1000
    Second number = 2000
    Addition of two numbers = 3000
    4
    First number = 1000
    Second number = 2000
    Addition of two numbers = 3000
    5    7 GeekforGeeks:0 GeekforGeeks:1
    First number = 1000
    Second number = 2000
    Addition of two numbers = 3000
    5
    GeekforGeeks
    1def2

        def def5

    GeekforGeeks
    1
    GeekforGeeks
    2

    GeekforGeeks
    14

    GeekforGeeks
    15

        def

    First number = 1000
    Second number = 2000
    Addition of two numbers = 3000
    0
    GeekforGeeks
    1
    GeekforGeeks
    2
     

    First number = 1000
    Second number = 2000
    Addition of two numbers = 3000

    Điều kiện tiên quyết - Trình xây dựng, @ClassMethod DecoratorsConstructors, @classmethod decorators

    Python không hỗ trợ nhiều hàm tạo rõ ràng, nhưng có một số cách sử dụng nhiều hàm tạo có thể đạt được. Nếu nhiều phương thức __init__ được viết cho cùng một lớp, thì loại mới nhất ghi đè lên tất cả các hàm tạo trước đó. Nhìn vào ví dụ dưới đây.__init__ methods are written for the same class, then the latest one overwrites all the previous constructors. Look at the example below.

    Python3

    class

    GeekforGeeks
    17

        def

    GeekforGeeks
    0
    GeekforGeeks
    1
    GeekforGeeks
    2

    GeekforGeeks
    3
    First number = 1000
    Second number = 2000
    Addition of two numbers = 3000
    4
    First number = 1000
    Second number = 2000
    Addition of two numbers = 3000
    5
    GeekforGeeks
    26
    GeekforGeeks
    13

        def

    GeekforGeeks
    0
    GeekforGeeks
    1
    GeekforGeeks
    2

    GeekforGeeks
    3
    First number = 1000
    Second number = 2000
    Addition of two numbers = 3000
    4
    First number = 1000
    Second number = 2000
    Addition of two numbers = 3000
    5
    GeekforGeeks
    36
    GeekforGeeks
    13

        def

    GeekforGeeks
    0
    GeekforGeeks
    1
    GeekforGeeks
    2

    GeekforGeeks
    3
    First number = 1000
    Second number = 2000
    Addition of two numbers = 3000
    4
    First number = 1000
    Second number = 2000
    Addition of two numbers = 3000
    5
    GeekforGeeks
    46
    GeekforGeeks
    13

    GeekforGeeks
    3
    First number = 1000
    Second number = 2000
    Addition of two numbers = 3000
    4
    First number = 1000
    Second number = 2000
    Addition of two numbers = 3000
    5
    GeekforGeeks
    36
    GeekforGeeks
    13

    GeekforGeeks48GeekforGeeks6 GeekforGeeks50

    Cần nhiều hàm tạo

    1. Nhiều hàm tạo được yêu cầu khi người ta phải thực hiện các hành động khác nhau trong việc khởi tạo một lớp. Điều này rất hữu ích khi lớp phải thực hiện các hành động khác nhau trên các tham số khác nhau. Các nhà xây dựng lớp có thể được thực hiện để thể hiện tính đa hình theo ba cách được liệt kê dưới đây.
    2. Quá tải các hàm tạo dựa trên các đối số.__init__.
    3. Gọi các phương thức từ __init__.@classmethod decorator.

    Sử dụng công cụ trang trí @ClassMethod.

    Bài viết này giải thích làm thế nào để có nhiều hàm tạo một cách sạch sẽ và pythonic với các ví dụ.

    Quá tải các hàm tạo dựa trên các đối sốsample

    • Quá tải hàm tạo được thực hiện bằng cách kiểm tra các điều kiện cho các đối số được thông qua và thực hiện các hành động cần thiết. Ví dụ: xem xét việc chuyển một đối số cho mẫu lớp, & nbsp;int, the square of the number should be the answer.
    • Nếu tham số là INT, bình phương của số phải là câu trả lời.String, the answer should be “Hello!!”+string.
    • Nếu tham số là một chuỗi, câu trả lời phải là Hello Hello !!length greater than 1, the sum of arguments should be stored as the answer.

    Python3

    Nếu tham số có độ dài lớn hơn 1, tổng các đối số nên được lưu trữ dưới dạng câu trả lời.

    class

    GeekforGeeks
    52

        def

    GeekforGeeks
    0
    GeekforGeeks
    1____111
    GeekforGeeks
    58
    GeekforGeeks
    59

    GeekforGeeks
    3
    GeekforGeeks
    61
    GeekforGeeks
    62
    GeekforGeeks
    63
    GeekforGeeks
    64
    GeekforGeeks
    65

    GeekforGeeks
    66
    GeekforGeeks
    1
    GeekforGeeks
    68
    GeekforGeeks
    6
    Sum of list : 15
    Square of int : 25
    String : Hello! GeeksforGeeks.
    
    7

    GeekforGeeks
    66
    GeekforGeeks
    72
    GeekforGeeks
    73
    GeekforGeeks
    74
    GeekforGeeks
    75

    GeekforGeeks
    76
    GeekforGeeks
    1
    GeekforGeeks
    68GeekforGeeks:0
    GeekforGeeks
    6
    GeekforGeeks
    81

    GeekforGeeks
    3
    GeekforGeeks
    83
    GeekforGeeks
    84
    GeekforGeeks
    85
    Sum of list : 15
    Square of int : 25
    String : Hello! GeeksforGeeks.
    
    7
    GeekforGeeks
    87
    GeekforGeeks
    88
    GeekforGeeks
    2

    GeekforGeeks
    66
    GeekforGeeks
    1
    GeekforGeeks
    68
    GeekforGeeks
    6
    GeekforGeeks
    94
    Sum of list : 15
    Square of int : 25
    String : Hello! GeeksforGeeks.
    
    7
    GeekforGeeks
    96
    GeekforGeeks
    58
    GeekforGeeks
    94
    Sum of list : 15
    Square of int : 25
    String : Hello! GeeksforGeeks.
    
    7__

    GeekforGeeks
    66
    GeekforGeeks
    1
    GeekforGeeks
    68
    GeekforGeeks
    6
    First number = 1000
    Second number = 2000
    Addition of two numbers = 3000
    13GeekforGeeks:0
    GeekforGeeks
    94
    Sum of list : 15
    Square of int : 25
    String : Hello! GeeksforGeeks.
    
    7
    GeekforGeeks
    96GeekforGeeks:0
    First number = 1000
    Second number = 2000
    Addition of two numbers = 3000
    19

    First number = 1000
    Second number = 2000
    Addition of two numbers = 3000
    20
    GeekforGeeks
    6
    First number = 1000
    Second number = 2000
    Addition of two numbers = 3000
    22222222222222222

    First number = 1000
    Second number = 2000
    Addition of two numbers = 3000
    4
    First number = 1000
    Second number = 2000
    Addition of two numbers = 3000
    5
    First number = 1000
    Second number = 2000
    Addition of two numbers = 3000
    35
    First number = 1000
    Second number = 2000
    Addition of two numbers = 3000
    36

    First number = 1000
    Second number = 2000
    Addition of two numbers = 3000
    37
    GeekforGeeks
    6
    First number = 1000
    Second number = 2000
    Addition of two numbers = 3000
    2222231
    GeekforGeeks
    13

    First number = 1000
    Second number = 2000
    Addition of two numbers = 3000
    4
    First number = 1000
    Second number = 2000
    Addition of two numbers = 3000
    5
    First number = 1000
    Second number = 2000
    Addition of two numbers = 3000
    44
    First number = 1000
    Second number = 2000
    Addition of two numbers = 3000
    45

    First number = 1000
    Second number = 2000
    Addition of two numbers = 3000
    46
    GeekforGeeks
    6
    First number = 1000
    Second number = 2000
    Addition of two numbers = 3000
    2222249
    GeekforGeeks
    13

    First number = 1000
    Second number = 2000
    Addition of two numbers = 3000
    4
    First number = 1000
    Second number = 2000
    Addition of two numbers = 3000
    5
    First number = 1000
    Second number = 2000
    Addition of two numbers = 3000
    53
    First number = 1000
    Second number = 2000
    Addition of two numbers = 3000
    54

    Đầu ra

    Sum of list : 15
    Square of int : 25
    String : Hello! GeeksforGeeks.
    

    Trong mã trên, biến thể hiện là ANS, nhưng các giá trị của nó khác nhau dựa trên các đối số. Vì một số lượng các đối số cho lớp, *Args được sử dụng là một bộ có chứa các đối số được truyền và có thể được truy cập bằng cách sử dụng một chỉ mục. Trong trường hợp của int và chuỗi, chỉ có một đối số được truyền và do đó được truy cập như ARGS [0] (phần tử duy nhất trong tuple).ans, but its values differ based on the arguments. Since a variable number of arguments for the class, *args is used which is a tuple that contains the arguments passed and can be accessed using an index. In the case of int and string, only one argument is passed and thus accessed as args[0] (the only element in the tuple).

    Gọi các phương thức từ __init__

    Một lớp có thể có một hàm tạo __init__ có thể thực hiện bất kỳ hành động nào khi thể hiện của lớp được tạo. Hàm tạo này có thể được thực hiện cho các chức năng khác nhau thực hiện các hành động khác nhau dựa trên các đối số được thông qua. Bây giờ hãy xem xét một ví dụ: & nbsp;

    • Nếu số lượng đối số được truyền là 2, thì hãy đánh giá biểu thức x = a2-b2x = a2-b2
    • Nếu số lượng đối số được truyền là 3, thì hãy đánh giá biểu thức y = a2+b2-c.y = a2+b2-c.
    • IFMORE so với 3 đối số đã được thông qua, sau đó tổng hợp các ô vuông, chia nó cho giá trị cao nhất trong các đối số được thông qua.more than 3 arguments have been passed, then sum up the squares, divide it by the highest value in the arguments passed.

    Python3

    class

    First number = 1000
    Second number = 2000
    Addition of two numbers = 3000
    56

        def

    GeekforGeeks
    0
    GeekforGeeks
    1____111
    GeekforGeeks
    58
    First number = 1000
    Second number = 2000
    Addition of two numbers = 3000
    63

    GeekforGeeks
    3
    GeekforGeeks
    61
    GeekforGeeks
    62
    First number = 1000
    Second number = 2000
    Addition of two numbers = 3000
    67
    GeekforGeeks
    6
    GeekforGeeks
    6
    First number = 1000
    Second number = 2000
    Addition of two numbers = 3000
    25
    GeekforGeeks
    65

    GeekforGeeks
    66
    GeekforGeeks
    1
    GeekforGeeks
    68
    GeekforGeeks
    6
    GeekforGeeks
    1
    First number = 1000
    Second number = 2000
    Addition of two numbers = 3000
    77

    GeekforGeeks
    3
    GeekforGeeks
    83
    GeekforGeeks
    62
    First number = 1000
    Second number = 2000
    Addition of two numbers = 3000
    67
    GeekforGeeks
    6
    GeekforGeeks
    6
    First number = 1000
    Second number = 2000
    Addition of two numbers = 3000
    27
    GeekforGeeks
    65

    GeekforGeeks
    66
    GeekforGeeks
    1
    GeekforGeeks
    68
    GeekforGeeks
    6
    GeekforGeeks
    1
    First number = 1000
    Second number = 2000
    Addition of two numbers = 3000
    91

    GeekforGeeks
    3
    First number = 1000
    Second number = 2000
    Addition of two numbers = 3000
    93
    GeekforGeeks
    65

    GeekforGeeks
    66
    GeekforGeeks
    1
    GeekforGeeks
    68
    GeekforGeeks
    6
    GeekforGeeks
    1
    Sum of list : 15
    Square of int : 25
    String : Hello! GeeksforGeeks.
    
    00

        def

    Sum of list : 15
    Square of int : 25
    String : Hello! GeeksforGeeks.
    
    03
    GeekforGeeks
    1____
    Sum of list : 15
    Square of int : 25
    String : Hello! GeeksforGeeks.
    
    05

    GeekforGeeks
    3____
    Sum of list : 15
    Square of int : 25
    String : Hello! GeeksforGeeks.
    
    07
    GeekforGeeks
    6
    GeekforGeeks
    85
    Sum of list : 15
    Square of int : 25
    String : Hello! GeeksforGeeks.
    
    7
    GeekforGeeks
    96
    GeekforGeeks
    58__

    GeekforGeeks
    3
    Sum of list : 15
    Square of int : 25
    String : Hello! GeeksforGeeks.
    
    29
    Sum of list : 15
    Square of int : 25
    String : Hello! GeeksforGeeks.
    
    30

        def

    Sum of list : 15
    Square of int : 25
    String : Hello! GeeksforGeeks.
    
    33
    GeekforGeeks
    1____
    Sum of list : 15
    Square of int : 25
    String : Hello! GeeksforGeeks.
    
    05

    GeekforGeeks
    3
    Sum of list : 15
    Square of int : 25
    String : Hello! GeeksforGeeks.
    
    37
    GeekforGeeks
    6
    GeekforGeeks
    85
    Sum of list : 15
    Square of int : 25
    String : Hello! GeeksforGeeks.
    
    7
    GeekforGeeks
    96__

    GeekforGeeks
    3
    Sum of list : 15
    Square of int : 25
    String : Hello! GeeksforGeeks.
    
    29
    Sum of list : 15
    Square of int : 25
    String : Hello! GeeksforGeeks.
    
    56

        def

    Sum of list : 15
    Square of int : 25
    String : Hello! GeeksforGeeks.
    
    59
    GeekforGeeks
    1
    Sum of list : 15
    Square of int : 25
    String : Hello! GeeksforGeeks.
    
    05

    GeekforGeeks
    3
    Sum of list : 15
    Square of int : 25
    String : Hello! GeeksforGeeks.
    
    63
    GeekforGeeks
    6
    Sum of list : 15
    Square of int : 25
    String : Hello! GeeksforGeeks.
    
    7

    Các

    GeekforGeeks
    66
    Sum of list : 15
    Square of int : 25
    String : Hello! GeeksforGeeks.
    
    63GeekforGeeks:0
    GeekforGeeks
    6
    Sum of list : 15
    Square of int : 25
    String : Hello! GeeksforGeeks.
    
    80
    GeekforGeeks
    58
    Sum of list : 15
    Square of int : 25
    String : Hello! GeeksforGeeks.
    
    80

    GeekforGeeks
    3
    Sum of list : 15
    Square of int : 25
    String : Hello! GeeksforGeeks.
    
    63
    GeekforGeeks
    6
    Sum of list : 15
    Square of int : 25
    String : Hello! GeeksforGeeks.
    
    86
    Sum of list : 15
    Square of int : 25
    String : Hello! GeeksforGeeks.
    
    87
    Sum of list : 15
    Square of int : 25
    String : Hello! GeeksforGeeks.
    
    88
    Sum of list : 15
    Square of int : 25
    String : Hello! GeeksforGeeks.
    
    89

    GeekforGeeks
    3
    Sum of list : 15
    Square of int : 25
    String : Hello! GeeksforGeeks.
    
    91
    GeekforGeeks
    6
    Sum of list : 15
    Square of int : 25
    String : Hello! GeeksforGeeks.
    
    86

    GeekforGeeks
    3
    Sum of list : 15
    Square of int : 25
    String : Hello! GeeksforGeeks.
    
    29
    Sum of list : 15
    Square of int : 25
    String : Hello! GeeksforGeeks.
    
    96

    Sum of list : 15
    Square of int : 25
    String : Hello! GeeksforGeeks.
    
    97
    GeekforGeeks
    6
    Sum of list : 15
    Square of int : 25
    String : Hello! GeeksforGeeks.
    
    99
    GeekforGeeks
    64
    GeekforGeeks
    11
    First number = 1000
    Second number = 2000
    Addition of two numbers = 3000
    25
    GeekforGeeks
    13

    equation 2 : -3
    equation 1 : 2
    equation 3 : 11.0
    
    04
    GeekforGeeks
    6
    Sum of list : 15
    Square of int : 25
    String : Hello! GeeksforGeeks.
    
    99
    GeekforGeeks
    64___

    equation 2 : -3
    equation 1 : 2
    equation 3 : 11.0
    
    13
    GeekforGeeks
    6
    Sum of list : 15
    Square of int : 25
    String : Hello! GeeksforGeeks.
    
    99
    GeekforGeeks
    64
    GeekforGeeks
    11
    First number = 1000
    Second number = 2000
    Addition of two numbers = 3000
    25
    GeekforGeeks
    11
    First number = 1000
    Second number = 2000
    Addition of two numbers = 3000
    27
    GeekforGeeks
    11__

    First number = 1000
    Second number = 2000
    Addition of two numbers = 3000
    4
    First number = 1000
    Second number = 2000
    Addition of two numbers = 3000
    5
    equation 2 : -3
    equation 1 : 2
    equation 3 : 11.0
    
    28
    equation 2 : -3
    equation 1 : 2
    equation 3 : 11.0
    
    29

    First number = 1000
    Second number = 2000
    Addition of two numbers = 3000
    4
    First number = 1000
    Second number = 2000
    Addition of two numbers = 3000
    5
    equation 2 : -3
    equation 1 : 2
    equation 3 : 11.0
    
    32
    equation 2 : -3
    equation 1 : 2
    equation 3 : 11.0
    
    33

    First number = 1000
    Second number = 2000
    Addition of two numbers = 3000
    4
    First number = 1000
    Second number = 2000
    Addition of two numbers = 3000
    5
    equation 2 : -3
    equation 1 : 2
    equation 3 : 11.0
    
    36
    equation 2 : -3
    equation 1 : 2
    equation 3 : 11.0
    
    37

    Đầu ra

    equation 2 : -3
    equation 1 : 2
    equation 3 : 11.0
    

    Trong mã trên, biến thể hiện là ANS, nhưng các giá trị của nó khác nhau dựa trên các đối số. Vì một số lượng các đối số cho lớp, *Args được sử dụng là một bộ có chứa các đối số được truyền và có thể được truy cập bằng cách sử dụng một chỉ mục. Trong trường hợp của int và chuỗi, chỉ có một đối số được truyền và do đó được truy cập như ARGS [0] (phần tử duy nhất trong tuple).

    Gọi các phương thức từ __init__

    Một lớp có thể có một hàm tạo __init__ có thể thực hiện bất kỳ hành động nào khi thể hiện của lớp được tạo. Hàm tạo này có thể được thực hiện cho các chức năng khác nhau thực hiện các hành động khác nhau dựa trên các đối số được thông qua. Bây giờ hãy xem xét một ví dụ: & nbsp;

    Nếu số lượng đối số được truyền là 2, thì hãy đánh giá biểu thức x = a2-b2

    Nếu số lượng đối số được truyền là 3, thì hãy đánh giá biểu thức y = a2+b2-c.

    IFMORE so với 3 đối số đã được thông qua, sau đó tổng hợp các ô vuông, chia nó cho giá trị cao nhất trong các đối số được thông qua.

    class

    First number = 1000
    Second number = 2000
    Addition of two numbers = 3000
    56classmethod is cls, which is like the selfof the instance methods. Here cls refer to the class itself. This proves to be very helpful to use multiple constructors in Python and is a more Pythonic approach considered to the above ones. Consider the same example used above. Evaluate different expressions based on the number of inputs.

    Python3

    class

    First number = 1000
    Second number = 2000
    Addition of two numbers = 3000
    56

        def

    GeekforGeeks
    0
    GeekforGeeks
    1____111
    GeekforGeeks
    58
    First number = 1000
    Second number = 2000
    Addition of two numbers = 3000
    63

    GeekforGeeks
    3
    GeekforGeeks
    1
    GeekforGeeks
    68
    GeekforGeeks
    6
    equation 2 : -3
    equation 1 : 2
    equation 3 : 11.0
    
    49

        

    equation 2 : -3
    equation 1 : 2
    equation 3 : 11.0
    
    51

        def

    Sum of list : 15
    Square of int : 25
    String : Hello! GeeksforGeeks.
    
    03
    equation 2 : -3
    equation 1 : 2
    equation 3 : 11.0
    
    55____
    Sum of list : 15
    Square of int : 25
    String : Hello! GeeksforGeeks.
    
    05

    GeekforGeeks
    3____
    Sum of list : 15
    Square of int : 25
    String : Hello! GeeksforGeeks.
    
    07
    GeekforGeeks
    6
    equation 2 : -3
    equation 1 : 2
    equation 3 : 11.0
    
    55
    equation 2 : -3
    equation 1 : 2
    equation 3 : 11.0
    
    61
    Sum of list : 15
    Square of int : 25
    String : Hello! GeeksforGeeks.
    
    7
    GeekforGeeks
    96
    GeekforGeeks
    58__

    GeekforGeeks
    3
    Sum of list : 15
    Square of int : 25
    String : Hello! GeeksforGeeks.
    
    29
    Sum of list : 15
    Square of int : 25
    String : Hello! GeeksforGeeks.
    
    30

        

    equation 2 : -3
    equation 1 : 2
    equation 3 : 11.0
    
    51

        def

    Sum of list : 15
    Square of int : 25
    String : Hello! GeeksforGeeks.
    
    33
    equation 2 : -3
    equation 1 : 2
    equation 3 : 11.0
    
    555
    Sum of list : 15
    Square of int : 25
    String : Hello! GeeksforGeeks.
    
    05

    GeekforGeeks
    3
    Sum of list : 15
    Square of int : 25
    String : Hello! GeeksforGeeks.
    
    37
    GeekforGeeks
    6
    equation 2 : -3
    equation 1 : 2
    equation 3 : 11.0
    
    55
    equation 2 : -3
    equation 1 : 2
    equation 3 : 11.0
    
    61
    Sum of list : 15
    Square of int : 25
    String : Hello! GeeksforGeeks.
    
    7
    GeekforGeeks
    96__

    GeekforGeeks
    3
    Sum of list : 15
    Square of int : 25
    String : Hello! GeeksforGeeks.
    
    29
    Sum of list : 15
    Square of int : 25
    String : Hello! GeeksforGeeks.
    
    56

        

    equation 2 : -3
    equation 1 : 2
    equation 3 : 11.0
    
    51

        def

    Sum of list : 15
    Square of int : 25
    String : Hello! GeeksforGeeks.
    
    59
    equation 2 : -3
    equation 1 : 2
    equation 3 : 11.0
    
    55
    Sum of list : 15
    Square of int : 25
    String : Hello! GeeksforGeeks.
    
    05

    GeekforGeeks
    3
    Sum of list : 15
    Square of int : 25
    String : Hello! GeeksforGeeks.
    
    63
    GeekforGeeks
    6
    Sum of list : 15
    Square of int : 25
    String : Hello! GeeksforGeeks.
    
    7

    Các

    GeekforGeeks
    66
    Sum of list : 15
    Square of int : 25
    String : Hello! GeeksforGeeks.
    
    63GeekforGeeks:0
    GeekforGeeks
    6
    Sum of list : 15
    Square of int : 25
    String : Hello! GeeksforGeeks.
    
    80
    GeekforGeeks
    58
    Sum of list : 15
    Square of int : 25
    String : Hello! GeeksforGeeks.
    
    80

    GeekforGeeks
    3
    Sum of list : 15
    Square of int : 25
    String : Hello! GeeksforGeeks.
    
    63
    GeekforGeeks
    6
    Sum of list : 15
    Square of int : 25
    String : Hello! GeeksforGeeks.
    
    86
    Sum of list : 15
    Square of int : 25
    String : Hello! GeeksforGeeks.
    
    87
    Sum of list : 15
    Square of int : 25
    String : Hello! GeeksforGeeks.
    
    88
    Sum of list : 15
    Square of int : 25
    String : Hello! GeeksforGeeks.
    
    89

    GeekforGeeks
    3
    Sum of list : 15
    Square of int : 25
    String : Hello! GeeksforGeeks.
    
    91
    GeekforGeeks
    6
    equation 2 : -3
    equation 1 : 2
    equation 3 : 11.0
    
    55
    equation 2 : -3
    equation 1 : 2
    equation 3 : 11.0
    
    51

    GeekforGeeks
    3
    Sum of list : 15
    Square of int : 25
    String : Hello! GeeksforGeeks.
    
    29
    Sum of list : 15
    Square of int : 25
    String : Hello! GeeksforGeeks.
    
    96

    equation 2 : -3
    equation 1 : 2
    equation 3 : 11.0
    
    555
    GeekforGeeks
    6
    equation 2 : -3
    equation 1 : 2
    equation 3 : 11.0
    
    57
    GeekforGeeks
    64___

    GeekforGeeks
    73
    GeekforGeeks
    6
    Sum of list : 15
    Square of int : 25
    String : Hello! GeeksforGeeks.
    
    7

    equation 2 : -3
    equation 1 : 2
    equation 3 : 11.0
    
    81
    equation 2 : -3
    equation 1 : 2
    equation 3 : 11.0
    
    82
    First number = 1000
    Second number = 2000
    Addition of two numbers = 3000
    27
    GeekforGeeks
    65

        

    equation 2 : -3
    equation 1 : 2
    equation 3 : 11.0
    
    86
    GeekforGeeks
    6
    equation 2 : -3
    equation 1 : 2
    equation 3 : 11.0
    
    88

    GeekforGeeks
    3
    equation 2 : -3
    equation 1 : 2
    equation 3 : 11.0
    
    98
    GeekforGeeks
    6 class00

    GeekforGeeks
    3
    First number = 1000
    Second number = 2000
    Addition of two numbers = 3000
    4
    First number = 1000
    Second number = 2000
    Addition of two numbers = 3000
    5
    equation 2 : -3
    equation 1 : 2
    equation 3 : 11.0
    
    28class05

        

    GeekforGeeks
    83
    GeekforGeeks
    62
    First number = 1000
    Second number = 2000
    Addition of two numbers = 3000
    67
    GeekforGeeks
    6
    GeekforGeeks
    6
    First number = 1000
    Second number = 2000
    Addition of two numbers = 3000
    27
    GeekforGeeks
    65

    GeekforGeeks
    3
    equation 2 : -3
    equation 1 : 2
    equation 3 : 11.0
    
    98
    GeekforGeeks
    6 class17

    GeekforGeeks
    3
    First number = 1000
    Second number = 2000
    Addition of two numbers = 3000
    4
    First number = 1000
    Second number = 2000
    Addition of two numbers = 3000
    5
    equation 2 : -3
    equation 1 : 2
    equation 3 : 11.0
    
    32class05

        

    First number = 1000
    Second number = 2000
    Addition of two numbers = 3000
    93
    GeekforGeeks
    65

    GeekforGeeks
    3
    equation 2 : -3
    equation 1 : 2
    equation 3 : 11.0
    
    98
    GeekforGeeks
    6 class29

    GeekforGeeks
    3
    First number = 1000
    Second number = 2000
    Addition of two numbers = 3000
    4
    First number = 1000
    Second number = 2000
    Addition of two numbers = 3000
    5
    equation 2 : -3
    equation 1 : 2
    equation 3 : 11.0
    
    36class05

        

    GeekforGeeks
    73GeekforGeeks:0
    GeekforGeeks
    6
    GeekforGeeks
    64

    Đầu ra

    equation 2 : -3
    equation 1 : 2
    equation 3 : 11.0
    

    Trong ví dụ trên, trường hợp của đối tượng không được tạo ban đầu. Các phương pháp lớp để đánh giá các biểu thức khác nhau đã được xác định với bộ trang trí @ClassMethod. Bây giờ chúng có thể được gọi với tên lớp và đối tượng được tạo trong phương thức lớp đó sau khi đánh giá biểu thức. Biến trường hợp giữ các câu trả lời khác nhau cho một số lượng tham số khác nhau được truyền.@classmethod decorator. Now they can be called with the class name and the object is created in that class method after evaluating the expression. The instance variable holds different answers for a different number of parameters passed.


    Chất xây dựng quá tải trong Python là gì?

    Quá tải hàm tạo được thực hiện bằng cách kiểm tra các điều kiện cho các đối số được thông qua và thực hiện các hành động cần thiết. Ví dụ: xem xét việc chuyển một đối số cho mẫu lớp, nếu tham số là INT, bình phương của số phải là câu trả lời.checking conditions for the arguments passed and performing required actions. For example, consider passing an argument to the class sample, If the parameter is an int, the square of the number should be the answer.

    Quá tải hàm tạo là gì giải thích với ví dụ là gì?

    Quá tải hàm tạo có thể được định nghĩa là khái niệm có nhiều hơn một hàm tạo với các tham số khác nhau để mọi hàm tạo có thể thực hiện một tác vụ khác nhau.Hãy xem xét chương trình Java sau đây, trong đó chúng tôi đã sử dụng các hàm tạo khác nhau trong lớp.the concept of having more than one constructor with different parameters so that every constructor can perform a different task. Consider the following Java program, in which we have used different constructors in the class.

    Quá tải hàm tạo là gì?

    Quá tải hàm tạo có nghĩa là có nhiều hơn một hàm tạo có cùng tên.Các hàm tạo là các phương thức được gọi khi một đối tượng được tạo.Bạn phải sử dụng cùng tên cho tất cả các hàm tạo là tên lớp.Điều này được thực hiện bằng cách khai báo hàm tạo với một số lượng đối số khác nhau.having more than one constructor with the same name. Constructors are methods invoked when an object is created. You have to use the same name for all the constructors which is the class name. This is done by declaration the constructor with a different number of arguments.

    Chất xây dựng trong Python với ví dụ là gì?

    Hàm tạo là một loại phương thức (hàm) đặc biệt được sử dụng để khởi tạo các thành viên thể hiện của lớp.Trong C ++ hoặc Java, hàm tạo có cùng tên với lớp của nó, nhưng nó đối xử với hàm tạo khác nhau trong Python.Nó được sử dụng để tạo một đối tượng.Người xây dựng có thể có hai loại.a special type of method (function) which is used to initialize the instance members of the class. In C++ or Java, the constructor has the same name as its class, but it treats constructor differently in Python. It is used to create an object. Constructors can be of two types.