python函数示例_Python中带示例的构造函数

本文介绍了Python中的构造函数,包括默认构造函数/非参数化构造函数和参数化构造函数。构造函数用于初始化对象的实例变量。在Python中,通过`__init__`方法实现构造函数。默认构造函数会在没有显式声明时自动创建,而参数化构造函数允许在创建对象时传入参数以初始化实例成员。
摘要由CSDN通过智能技术生成

python函数示例

A Constructor is the special method (function) used to initialize the instance variables during object creation.

构造函数是用于在对象创建期间初始化实例变量的特殊方法(函数)。

Types of constructors in Python

Python中的构造函数类型

  1. Default Constructor /Non parameterized Constructor

    默认构造函数/非参数化构造函数

  2. 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, this in C++).

在这里, __init__用于类的构造函数。 self参数引用对象的实例(例如C ++中的对象)。

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 constructor
Sum = Addition()

# calling the instance method 
# using the object Sum
Sum.result()

Output

输出量

Output: 6000

1)Python默认构造函数/未参数化 (1) Python Default constructor/Non Parameterized)

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 constructor
Sum = Addition()

# calling the instance method 
# using the object Sum
Sum.result()

Output

输出量

Output: 6000

2)Python参数化构造函数 (2) Python Parameterized Constructor)

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 constructor
student=Student("Yash",2023,"Kcc")

# calling the instance method 
# using the object student
student.Display_Details()

Output

输出量

This is a parmeterized costructor in python:
Student Details:
Student Name: Yash
Student ids: 2023
Student college: Kcc


翻译自: https://www.includehelp.com/python/constructors-with-examples.aspx

python函数示例

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值