本文共 4281 字,大约阅读时间需要 14 分钟。
python函数示例
A Constructor is the special method (function) used to initialize the instance variables during object creation.
构造函数是用于在对象创建期间初始化实例变量的特殊方法(函数)。
Types of constructors in Python
Python中的构造函数类型
Default Constructor /Non parameterized Constructor
默认构造函数/非参数化构造函数
Parameterized Constructor
参数化构造函数
How to create the Constructors?
如何创建构造函数?
In python, __init__ method stimulates the constructor of the class.
在python中, __init__方法会刺激该类的构造函数。
When we instantiate the class we call this method. It generally used when we initialize the class attributes. It is a must for a class to have the constructor, even if they rely on the default constructor.
当我们实例化该类时,我们调用此方法。 通常在初始化类属性时使用。 一个类必须具有构造函数,即使它们依赖于默认构造函数也是如此。
Syntax to define a constructor:
定义构造函数的语法:
While declaring the constructor one important thing is Constructor always has init and the word init is having two underscores ( __ ) before and after it, Like: __init__
在声明构造函数时,重要的一件事是Constructor始终具有init ,而init一词在其前后都有两个下划线( __ ),例如: __init__
We declare the constructor using def Keyword.
我们使用def Keyword 声明构造函数 。
Syntax:
句法:
def __init__(self): #now all about the constructor
Here, __init__ is used for the constructor to a class. The self parameter refers to the instance of the object (like, ).
在这里, __init__用于类的构造函数。 self参数引用对象的实例(例如中的对象)。
Example:
例:
class Addition: # Defininf a constructor def __init__(self): # with the help of self.xyz # we are initializing instance variable self.num1=1000 self.num2=2000 self.num3=3000 def result(self): self.num=self.num1+self.num2+self.num3 print('Output:',self.num)# Here we create the object for call # which calls the constructorSum = Addition()# calling the instance method # using the object SumSum.result()
Output
输出量
Output: 6000
In this above example, we are using a default constructor to assigning the default values to the variables num1, num2 and num3.
在上面的示例中,我们使用默认构造函数为变量num1 , num2和num3分配默认值。
More about the default/non parameterized constructor...
有关默认/非参数化构造函数的更多信息...
We cannot create the object if we don’t have a constructor in our program. This is why when we do not declare the constructor in our program python having the inbuilt feature it does it for us.
如果程序中没有构造函数,则无法创建对象。 这就是为什么当我们不在程序python中声明具有内置功能的构造函数时,它会为我们执行此操作的原因。
For example: In the given below program we do not have the constructor but still, we can create the object. This is because of the default property of python it implicitly does that during compilation.
例如:在下面给出的程序中,我们没有构造函数,但仍然可以创建对象。 这是因为python的默认属性,它在编译期间隐式地执行了该操作。
Default constructor looks like this:
默认构造函数如下所示:
def __init__(self): # no body, does nothing.
Example:
例:
class Addition: num1=1000 num2=2000 num3=3000# here as we have no constructor # so it uses the default one. def result(self): self.num=self.num1+self.num2+self.num3 print('Output:',self.num)# Here we create the object for call # which calls the constructorSum = Addition()# calling the instance method # using the object SumSum.result()
Output
输出量
Output: 6000
When we declare a constructor in the way that accepts the arguments during the object creation these type of constructors are called the Parameterized Constructors.
当我们以在对象创建过程中接受参数的方式声明构造函数时,这些类型的构造函数称为“ 参数化构造函数” 。
This is used to initialize the instance members of the object.
这用于初始化对象的实例成员。
Example:
例:
class Student: # Defining a parameterized constructor having arguments def __init__(self,name,ids,college): print("This is a parmeterized costructor in python:") self.name=name self.ids=ids self.college=college def Display_Details(self): print("Student Details:") print("Student Name:",self.name) print("Student ids:",self.ids) print("Student college:",self.college)# Here we create the object for call # which calls the constructorstudent=Student("Yash",2023,"Kcc")# calling the instance method # using the object studentstudent.Display_Details()
Output
输出量
This is a parmeterized costructor in python:Student Details:Student Name: YashStudent ids: 2023Student college: Kcc
翻译自:
python函数示例
转载地址:http://icvzd.baihongyu.com/