博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
python函数示例_Python中带示例的构造函数
阅读量:2527 次
发布时间:2019-05-11

本文共 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中的构造函数类型

  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, ).

在这里, __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

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 constructorSum = Addition()# calling the instance method # using the object SumSum.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 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/

你可能感兴趣的文章
[USACO18DEC]The Cow Gathering
查看>>
情感分析-英文电影评论
查看>>
王者荣耀游戏服务器架构的演进读后感
查看>>
关于ajax请求controller返回中文乱码的解决方法!
查看>>
Objective-C 和 Core Foundation 对象相互转换的内存管理总结
查看>>
IOS音频1:之采用四种方式播放音频文件(一)AudioToolbox AVFoundation OpenAL AUDIO QUEUE...
查看>>
Linux nmon 命令
查看>>
使用 urllib 构造请求对象
查看>>
sql server book
查看>>
长亭技术专栏 安全攻防技术分享
查看>>
sql server dba
查看>>
visualvm
查看>>
docker(4):coreos+docker+rancher真厉害
查看>>
设计模式之代理模式,学习笔记
查看>>
在Qsys中创建用户自定义IP
查看>>
【leetcode】Container With Most Water
查看>>
Python -- machine learning, neural network -- PyBrain 机器学习 神经网络
查看>>
一道dp题目
查看>>
mysql中间件研究(tddl atlas cobar sharding-jdbc)
查看>>
Cast-128 加密算法和 MyPassWord 的破解
查看>>