资讯

精准传达 • 有效沟通

从品牌网站建设到网络营销策划,从策略到执行的一站式服务

Python中class和对象基本用法是什么-创新互联

本篇文章为大家展示了Python中class和对象基本用法是什么,内容简明扼要并且容易理解,绝对能使你眼前一亮,通过这篇文章的详细介绍希望你能有所收获。

创新互联公司专注于银海企业网站建设,成都响应式网站建设公司,商城网站定制开发。银海网站建设公司,为银海等地区提供建站服务。全流程按需网站制作,专业设计,全程项目跟踪,创新互联公司专业和态度为您提供的服务

主要介绍了Python 面向对象之类class和对象基本用法,结合实例形式详细分析了Python面向对象程序设计中类class和对象基本概念、原理、使用方法与操作注意事项,需要的朋友可以参考下。

类(class):定义一件事物的抽象特点,usually,类定义了事物的属性和它可以做到的性为对象(object):是类的实例。

1.基本点

< code class="prism language-bash">class MyClass(object):

message = "hello,world"

def show(self):

print (self.message)

< /code>

类名为MyClass 有一个成员变量:message,并赋予初值。

类中定义了成员函数show(self),注意类中的成员函数必须带有参数self。

参数self是对象本身的引用,在成员函数中可以引用self参数获得对象的信息。

输出结果:

< code class="prism language-bash">inst = Myclass() # 实例化一个MyClass 的对象

inst.show # 调用成员函数,无需传入self参数

hello,world

< /code>

注: 通过在类名后面加小括号可以直接实例化类来获得对象变量,使用对象变量可以访问类的成员函数与成员变量。

2.构造函数

构造函数是一种特殊的类成员方法,主要用来创建对象初始化,python 中的类构造函数用__init__命名:

< code class="prism language-bash">class MyClass(object):

message = 'Hello, Developer.'

def show(self):

print self.message

def __init__(self):

print "Constructor is called"

inst = MyClass()

inst.show()

>>>

< /code>

打印结果:

< code class="prism language-bash">>>>Constructor is called

>>>Hello, Developer.

< /code>

注:构造函数不能有返回值,python 中不能定义多个构造函数,但可以通过为命名参数提供默认值的方式达到用多种方式构造对象的目的。

3.析构函数

是构造的反向函数,在销毁或者释放对象时调用他们。

python 中为类定义析构函数的方法在类定义中定义一个名为__del__的没有返回值和参数的函数。

< code class="prism language-bash">class MyClass(object):

message = 'Hello, Developer.'

def show(self):

print self.message

def __init__(self, name = "unset", color = "black"):

print "Constructor is called with params: ",name, " ", color

def __del__(self):

print "Destructor is called!"

inst = MyClass()

inst.show()

inst2 = MyClass("David")

inst2.show()

del inst, inst2

inst3 = MyClass("Lisa", "Yellow")

inst3.show()

del inst3

>>>

< /code>

4.实例成员变量

构造函数中定义self引用的变量,因此这样的成员变量在python中叫做实例成员变量。

< code class="prism language-bash">def __init__(self, name = "unset", color = "black"):

print "Constructor is called with params: ",name, " ", color

self.name = name

self.color = color

< /code>

5.静态函数和类函数:

python 支持两种基于类名访问成员的函数:静态函数,类函数。

区别在于:类函数有一个隐形参数cls可以用来获取类信息。而静态函数没有该函数。

静态函数用装饰器:@staticmethod定义

类函数使用装饰器:@classmethod定义

< code class="prism language-bash">class MyClass(object):

message = 'Hello, Developer.'

def show(self):

print (self.message)

print ("Here is %s in %s!" % (self.name, self.color))

@staticmethod

def printMessage():

print ("printMessage is called")

print (MyClass.message)

@classmethod

def createObj(cls, name, color):

print ("Object will be created: %s(%s, %s)"% (cls.__name__, name, color))

return cls(name, color)

def __init__(self, name = "unset", color = "black"):

print ("Constructor is called with params: ",name, " ", color)

self.name = name

self.color = color

def __del__(self):

print ("Destructor is called for %s!"% self.name)

MyClass.printMessage()

inst = MyClass.createObj( "Toby", "Red")

print (inst.message)

del inst

< /code>

6.私有成员

python 使用指定变量名格式的方法定义私有成员,即所有以双下划线“__”开始命名的成员都为私有成员。

< code class="prism language-bash">class MyClass(object):

def __init__(self, name = "unset", color = "black"):

print "Constructor is called with params: ",name, " ", color

self.__name = name

self.__color = color

def __del__(self):

print "Destructor is called for %s!"% self.__name

inst = MyClass("Jojo", "White")

del inst

< /code>

上述内容就是Python中class和对象基本用法是什么,你们学到知识或技能了吗?如果还想学到更多技能或者丰富自己的知识储备,欢迎关注创新互联行业资讯频道。


本文名称:Python中class和对象基本用法是什么-创新互联
URL标题:http://cdkjz.cn/article/jsjsp.html
多年建站经验

多一份参考,总有益处

联系快上网,免费获得专属《策划方案》及报价

咨询相关问题或预约面谈,可以通过以下方式与我们联系

业务热线:400-028-6601 / 大客户专线   成都:13518219792   座机:028-86922220