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

所属分类: 脚本专栏 / python 阅读数: 291
收藏 0 赞 0 分享

本文实例讲述了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基于Image模块实现图像翻转、旋转、改变大小等操作的相关技巧,非常简单实用,需要的朋友可以参考下
收藏 0 赞 0 分享

Python中的urllib模块使用详解

这篇文章主要介绍了Python中的urllib模块使用详解,是Python入门学习中的基础知识,需要的朋友可以参考下
收藏 0 赞 0 分享

Python的多态性实例分析

这篇文章主要介绍了Python的多态性,以实例形式深入浅出的分析了Python在面向对象编程中多态性的原理与实现方法,需要的朋友可以参考下
收藏 0 赞 0 分享

python生成IP段的方法

这篇文章主要介绍了python生成IP段的方法,涉及Python文件读写及随机数操作的相关技巧,具有一定参考借鉴价值,需要的朋友可以参考下
收藏 0 赞 0 分享

python操作redis的方法

这篇文章主要介绍了python操作redis的方法,包括Python针对redis的连接、设置、获取、删除等常用技巧,具有一定参考借鉴价值,需要的朋友可以参考下
收藏 0 赞 0 分享

python妹子图简单爬虫实例

这篇文章主要介绍了python妹子图简单爬虫,实例分析了Python爬虫程序所涉及的页面源码获取、进度显示、正则匹配等技巧,需要的朋友可以参考下
收藏 0 赞 0 分享

分析用Python脚本关闭文件操作的机制

这篇文章主要介绍了分析用Python脚本关闭文件操作的机制,作者分Python2.x版本和3.x版本两种情况进行了阐述,需要的朋友可以参考下
收藏 0 赞 0 分享

python实现搜索指定目录下文件及文件内搜索指定关键词的方法

这篇文章主要介绍了python实现搜索指定目录下文件及文件内搜索指定关键词的方法,可实现针对文件夹及文件内关键词的搜索功能,需要的朋友可以参考下
收藏 0 赞 0 分享

python中getaddrinfo()基本用法实例分析

这篇文章主要介绍了python中getaddrinfo()基本用法,实例分析了Python中使用getaddrinfo方法进行IP地址解析的基本技巧,需要的朋友可以参考下
收藏 0 赞 0 分享

python查找指定具有相同内容文件的方法

这篇文章主要介绍了python查找指定具有相同内容文件的方法,涉及Python针对文件操作的相关技巧,需要的朋友可以参考下
收藏 0 赞 0 分享
查看更多