zoukankan      html  css  js  c++  java
  • Python Object Oriented

    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 instance
    e = 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 exists
    getattr(emp1, 'age')    # Returns value of 'age' attribute
    setattr(emp1, 'age', 8) # Set attribute 'age' at 8
    delattr(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 = pt1
    pt3 = pt1
    print id(pt1), id(pt2), id(pt3) # prints the ids of the obejcts
    del pt1
    del pt2
    del pt3
    
    #result
    3083401324 3083401324 3083401324
    Point destroyed
    

      

    5. inheritance

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

      

  • 相关阅读:
    Java线程池,你了解多少?
    Git-常用命令
    CentOS6.6 编译Redis报错:"Newer version of jemalloc required"
    IDEA 常用快捷键
    Java并发编程(4)--生产者与消费者模式介绍
    CSS样式----浮动(图文详解)
    实现键盘记录的e.Whick和keyCode,兼容FireFox和IE
    如何用Fireworks制作经典的扫光字GIF动画
    asp.net中label控件设置字体大小
    [HttpException (0x80004005): 应用程序已预编译,因此不允许使用目录“/App_Code/”。]
  • 原文地址:https://www.cnblogs.com/KennyRom/p/6297564.html
Copyright © 2011-2022 走看看