Python 面向对象之类class和对象基本用法示例

 更新时间:2020年02月02日 11:44:09   作者:Dawn__Z  
这篇文章主要介绍了Python 面向对象之类class和对象基本用法,结合实例形式详细分析了Python面向对象程序设计中类class和对象基本概念、原理、使用方法与操作注意事项,需要的朋友可以参考下

本文实例讲述了Python 面向对象之类class和对象基本用法。分享给大家供大家参考,具体如下:

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

对象(object):是类的实例。

1.基本点

class MyClass(object):
  message = "hello,world"
  def show(self):
    print (self.message)

类名为MyClass 有一个成员变量:message,并赋予初值
类中定义了成员函数show(self),注意类中的成员函数必须带有参数self
参数self是对象本身的引用,在成员函数中可以引用self参数获得对象的信息

输出结果:

inst = Myclass() # 实例化一个MyClass 的对象
inst.show # 调用成员函数,无需传入self参数
hello,world

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

2.构造函数

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

class MyClass(object):
  message = 'Hello, Developer.'
  def show(self):
    print self.message
  def __init__(self):
    print "Constructor is called"
inst = MyClass()
inst.show()
>>>

打印结果:

>>>Constructor is called
>>>Hello, Developer.

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

3.析构函数

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

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

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
>>>

打印结果:

Constructor is called with params:  unset   black
Hello, Developer.
Constructor is called with params:  David   black
Hello, Developer.
Destructor is called!
Destructor is called!
Constructor is called with params:  Lisa   Yellow
Hello, Developer.
Destructor is called!

4.实例成员变量

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

def __init__(self, name = "unset", color = "black"):
  print "Constructor is called with params: ",name, " ", color
  self.name = name
  self.color = color

5.静态函数和类函数:

python 支持两种基于类名访问成员的函数:静态函数,类函数。
区别在于:类函数有一个隐形参数cls可以用来获取类信息。而静态函数没有该函数。
静态函数用装饰器:@staticmethod定义
类函数使用装饰器:@classmethod定义

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

输出结果:

printMessage is called
Hello, Developer.
Object will be created: MyClass(Toby, Red)
Constructor is called with params:  Toby   Red
Hello, Developer.
Destructor is called for Toby!

6.私有成员

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

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

输出结果:

Constructor is called with params:  Jojo   White
Destructor is called for Jojo!

注明:书《Python 高效开发实战Django, Tornado, Flask, Twisted》总结

更多关于Python相关内容感兴趣的读者可查看本站专题:《Python面向对象程序设计入门与进阶教程》、《Python数据结构与算法教程》、《Python函数使用技巧总结》、《Python字符串操作技巧汇总》、《Python编码操作技巧总结》及《Python入门与进阶经典教程

希望本文所述对大家Python程序设计有所帮助。

相关文章

  • python如何实现质数求和

    python如何实现质数求和

    这篇文章主要介绍了python如何实现质数求和,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
    2022-05-05
  • Python中xlsx文件转置操作详解(行转列和列转行)

    Python中xlsx文件转置操作详解(行转列和列转行)

    很多时候我们处理的Excel表格并不是我们想要的样子,需要将表格的形式进行相应转换后进行数据分析操作,下面这篇文章主要给大家介绍了关于Python中xlsx文件转置操作(行转列和列转行)的相关资料,需要的朋友可以参考下
    2022-07-07
  • Python去除列表中重复元素的方法

    Python去除列表中重复元素的方法

    这篇文章主要介绍了Python去除列表中重复元素的方法,实例分析了Python中去除列表重复元素的使用技巧,需要的朋友可以参考下
    2015-03-03
  • python代码实现学生信息管理系统

    python代码实现学生信息管理系统

    这篇文章主要为大家详细介绍了python代码实现学生信息管理系统,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2022-05-05
  • Python 多线程共享变量的实现示例

    Python 多线程共享变量的实现示例

    这篇文章主要介绍了Python 多线程共享变量的实现示例,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2020-04-04
  • Python简单生成随机姓名的方法示例

    Python简单生成随机姓名的方法示例

    这篇文章主要介绍了Python简单生成随机姓名的方法,结合实例形式分析了Python基于random模块生成随机字符串组合的相关操作技巧,需要的朋友可以参考下
    2017-12-12
  • Python读写常用数据文件的示例详解

    Python读写常用数据文件的示例详解

    Python 提供了多种强大的工具和库,可以轻松实现对各种类型文件的读写操作,本文为大家整理了Python读写常用的那些数据文件的方法,希望对大家有所帮助
    2025-01-01
  • Python第三方库安装缓慢的解决方法

    Python第三方库安装缓慢的解决方法

    这篇文章主要给大家介绍了关于Python第三方库安装缓慢的解决方法,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2021-02-02
  • 基于TensorFlow中自定义梯度的2种方式

    基于TensorFlow中自定义梯度的2种方式

    今天小编就为大家分享一篇基于TensorFlow中自定义梯度的2种方式,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
    2020-02-02
  • pyTorch深度学习多层感知机的实现

    pyTorch深度学习多层感知机的实现

    这篇文章主要为大家介绍了pyTorch深度学习多层感知机的实现,文中附含详细示例代码,有需要的朋友可以借鉴参考下,希望能够有所帮
    2021-09-09

最新评论