博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Python Object Oriented
阅读量:7224 次
发布时间:2019-06-29

本文共 2589 字,大约阅读时间需要 8 分钟。

1. Creating class

class className:   'Optional class documentation string'   class_suite

  

The class has a documentation string, which can be accessed via ClassName.__doc__.

 

Example:

class Employee:   'Common base class for all employees'   empCount = 0   def __init__(self, name, salary):      self.name = name      self.salary = salary      Employee.empCount += 1   #class constructor or initialization method   #It's used to make a new class instance   #the first argument to each method is self   #When we use this method, we do not need to add it. Python will adds the self argument to the list for you.   def displayCount(self):     print("Total Employee %d" % Employee.empCount)   def displayEmployee(self):      print("Name : ", self.name,  ", Salary: ", self.salary)#create an instancee = Employee('Ben', 1000)

  

2.  access an attribute

just like C++

#We can add, remove, or modify attributes of classes and objects at any time.emp1.age = 7  # Add an 'age' attribute.emp1.age = 8  # Modify 'age' attribute.del emp1.age  # Delete 'age' attribute.

  

hasattr(emp1, 'age')    # Returns true if 'age' attribute existsgetattr(emp1, 'age')    # Returns value of 'age' attributesetattr(emp1, 'age', 8) # Set attribute 'age' at 8delattr(empl, 'age')    # Delete attribute 'age'

  

3. Built-in class attribute

 

ictionary containing the class's namespace.__name__ #Class name.__module__ #Module name in which the class is defined. This attribute is "__main__" in interactive mode.__bases__ #A possibly empty tuple containing the base classes, in the order of their occurrence in the base class list.

  

4. destroy

#Python deletes unneeded objects (built-in types or class instances) automatically to free the memory space. '''An object's reference count increases when it is assigned a new name or placed in a container (list, tuple, or dictionary). The object's reference count decreases when it's deleted with del, its reference is reassigned, or its reference goes out of scope. When an object's reference count reaches zero, Python collects it automatically.'''class Point:   def __init( self, x=0, y=0):      self.x = x      self.y = y   def __del__(self):      class_name = self.__class__.__name__      print class_name, "destroyed"pt1 = Point()pt2 = pt1pt3 = pt1print id(pt1), id(pt2), id(pt3) # prints the ids of the obejctsdel pt1del pt2del pt3#result3083401324 3083401324 3083401324Point destroyed

  

5. inheritance

class SubClassName (ParentClass1[, ParentClass2, ...]):   'Optional class documentation string'   class_suite

  

 

转载于:https://www.cnblogs.com/KennyRom/p/6297564.html

你可能感兴趣的文章
FreeWheel业务系统微服务化过程经验分享
查看>>
移动互联网下半场,iOS开发者如何“高薪”成长?
查看>>
Atlassian是怎样进行持续交付的?且听 Steve Smith一一道来
查看>>
Web Storage相关
查看>>
[PHP内核探索]PHP中的哈希表
查看>>
Apache-drill Architechture
查看>>
WordPress 5.2 Beta 3 发布,要求 PHP 5.6.20 以上版本
查看>>
通通连起来——无处不在的流
查看>>
互联网+时代,看云计算如何改变传统行业
查看>>
ZFS ARC & L2ARC zfs-$ver/module/zfs/arc.c
查看>>
c++类默认拷贝构造函数---浅复制
查看>>
2019年最火热的Golang项目
查看>>
可实现RSSD云硬盘120万IOPS的SPDK IO路径优化实践
查看>>
Vue项目部署遇到的坑(你肯定会遇到!)
查看>>
资源分享计划第三期 0511
查看>>
awk 文本处理
查看>>
【JSConf EU 2018】主题总结 (部分主题已有中文文章)
查看>>
JavaScript面向对象名词详解
查看>>
Java设计模式学习 - 责任链模式
查看>>
JVM,DVM,ART
查看>>