zoukankan      html  css  js  c++  java
  • python class 1

    //test.py

    class Employee:
      'all employee'
      empCount = 0
      def __init__(self, name, salary):
        self.name = name
        self.salary = salary
        Employee.empCount += 1
      def prt(self):
        print 'self ', self
        print '__class__', self.__class__
      def displayCount(self):
        print 'Total Employee %d' % Employee.empCount
      def displayEmployee(self):
        print 'Name: ', self.name, ', Salary: ', self.salary

    ee = Employee('zcl', 0)
    print 'doc', Employee.__doc__
    ee.displayCount()
    ee.displayEmployee()
    ee.prt()
    print 'hasattr', hasattr(ee, 'empCount'), ee.empCount
    print 'getattr', getattr(ee, 'empCount')
    print 'setattr', setattr(ee, 'empCount', 2), ee.empCount
    print 'delattr', delattr(ee, 'empCount')
    print 'hasattr', hasattr(ee, 'empCount')

    //result

    # python test.py
    doc all employee
    Total Employee 1
    Name: zcl , Salary: 0
    self <__main__.Employee instance at 0x7f4564a60b48>
    __class__ __main__.Employee
    hasattr True 1
    getattr 1
    setattr None 2
    delattr None
    hasattr True

    Finally:

    这个例子告诉我们,类的属性是不能删除的

  • 相关阅读:
    C++ 对象没有显式初始化
    NFA与DFA
    VS DLL 复制本地
    TFS 图标意思
    C++ 析构方法
    C++ 异常
    【转】二叉树的非递归遍历
    【转】Dijkstra算法(单源最短路径)
    Dijkstra最短路径算法
    python __name__
  • 原文地址:https://www.cnblogs.com/woodzcl/p/7808782.html
Copyright © 2011-2022 走看看