用python面向对象创建宝贵的类示例相关介绍


本文主要介绍的是python面向对象的实际应用方案的相关介绍,以及python面向对象中的字符串的相关应用的介绍,如果你对其的实际的相关应用感兴趣的话,你就可以点击以下的文章对其进行了解。

概述

采用面向对象编程之后程序员的重点是放在创建他们自定义的类型上即类。类可以称为程序员自定义类型。每个类都包含了数据和一系统数据处理函数。尽最大可能实现代码的重用重用可重用!要创建宝贵的类示例:

  1. #-*- coding: utf-8 -*-  
  2. class Time:  
  3. def __init__(self):  
  4. self.hour = 0 
  5. self.minute = 0 
  6. self.second = 0 
  7. def printStandard(self):  
  8. standardTime = "" 
  9. if self.hour == 0 or self.hour == 12:  
  10. standardTime += "12:"  
  11. else:  
  12. standardTime += "%d" % (self.hour % 12)  
  13. standardTime += "%.2d:%.2d" % (self.minute,self.second)  
  14. if self.hour < 12: 
  15. standardTime += "AM"  
  16. else:  
  17. standardTime += "PM"  
  18. print standardTime   

Python中定义了几个特殊方法,这些特殊方法之前与之后都有双下划线(__)就是定义特殊的方法!其中的:self 表示对象引用参数!PS2:不建议直接访问对象的属性。以下列出python对象提供有关于自已的信息,通过这些信息反馈了python 面向对象的内省功能。

__bases__ 包含基类的一个元组,类可从这些基类直接继承。如果类不从其他类继承,元组就会为空 。示例:print Time.__bases__ 打印当前类的父类__dict__ 与类的命名空间对应的一个字典。其中每个键-值对都代表在命名空间中的一个标识符及值 示例:

  1. print Time.__dict__  

输出


  1. :{'__module__': '__main__', 'printStandard':
     
    <function printStandard at 0x00AD23B0>, '__dict__': 
    <attribute '__dict__' of 'Time' objects>, '__weakref__':
     
    <attribute http://new.51cto.com/wuyou/'__weakref__' of 'Time' objects>, '__doc__':
     None, '__init__': 
    <function __init__ at 0x00AD2370>

__doc__ 类的文档化字符串。如果类没有指定文档化字符串值为None__module__ 包含模块文件)名的一个字符串,类定义在这个模块中示例: 打印输出 __main____name__ 包含类名的一个字符串。

  1. <!--[if !supportLists]--> 

python面向对象的实际应用的部分介绍。

相关内容

    暂无相关文章

评论关闭