python 대상 노트

4939 단어
class Employee:
   '       '
   **empCount = 0** #   ,         , Java static  ,             
   def __init__(self, name, salary):#self     
    #self.name、self.salary       Java public    ,  “   .   “     
      self.name = name  
      self.salary = salary
      **Employee.empCount** += 1 #        :  .   

   def displayCount(self):
     print "Total Employee %d" % Employee.empCount

   def displayEmployee(self):
  print"Name:",self.name,",Salary:",self.salary



pyCharm .py  :
em=Employee("liu",20) #     self       :_init_   
em.displayCount()  #  :Total Employee 2
em.displayEmployee() #  :Name: liu ,Salary: 20
en=Employee("li",21) #  :_init_  
en.displayCount()    #  :Total Employee 3
en.displayEmployee() #  :Name: li ,Salary: 21
print em.salary      #  :20
print en.salary      #  :21
print em.empCount    #  :3
print en.empCount    #  :3def displayEmployee(self):
        print "Name:",self.name,",Salary:",self.salary
        if(hasattr(self,'age')):#    age   
            print self.age

  :
em.age=12 #     em    age
en.displayEmployee()#  :Name: li ,Salary: 21
em.displayEmployee()#  :Name: liu ,Salary: 20 12
del em.age #      
em.displayEmployee() #  :Name: liu ,Salary: 20def displayEmployee(self):
        print "Name:",self.name,",Salary:",self.salary
        if(hasattr(Employee,'age')):
            print Employee.age
            Employee.age+=1

   :
em.age=22222
print em.displayEmployee()
#  :Name: liu,Salary: 20 
None
print en.displayEmployee()
#  :Name: li ,Salary: 21
None


     :
Employee.age=22222
print em.displayEmployee()
#  :Name: liu ,Salary: 20
22222 #########
None??       
print en.displayEmployee()
#  :Name: li ,Salary: 21
22223  ########
None??       

좋은 웹페이지 즐겨찾기