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:

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

  • 相关阅读:
    Activity的启动模式
    Activity的生命周期
    C之静态内存和动态内存
    C之指针的加法
    C之函数返回一个以上的值
    C之交换数据案例
    C之自定义类型
    C之枚举
    联合体
    C之结构体
  • 原文地址:https://www.cnblogs.com/woodzcl/p/7808782.html
Copyright © 2011-2022 走看看