码迷,mamicode.com
首页 > 编程语言 > 详细

python class 1

时间:2017-11-09 15:00:43      阅读:152      评论:0      收藏:0      [点我收藏+]

标签:类的属性   doc   none   self   elf   main   setattr   attr   count   

//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:

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

python class 1

标签:类的属性   doc   none   self   elf   main   setattr   attr   count   

原文地址:http://www.cnblogs.com/woodzcl/p/7808782.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!