Python Class Foundation

 

  
  
  
  
  1. # -*- coding: cp936 -*- 
  2. class Person: 
  3.      
  4.     """代表一个人""" 
  5.      
  6.     population = 0 
  7.  
  8.     def __init__(self,name): 
  9.         """初始化""" 
  10.         self.name = name 
  11.         print "初始化,%s" % self.name 
  12.         Person.population +=1 
  13.  
  14.     def __del__(self): 
  15.         "I 'm dieing" 
  16.         print "%s says byebye" %self.name 
  17.         Person.population -= 1 
  18.  
  19.         if Person.population == 0
  20.             print "no one left" 
  21.         elif Person.population >=1
  22.             print "there are %s person left"  %Person.population 
  23.  
  24.     def sayHi(self): 
  25.         """person say hi""" 
  26.         print "%s says hi" % self.name 
  27.  
  28.     @staticmethod 
  29.     def howMany(self): 
  30.         """how many person left""" 
  31.         if Person.population ==1
  32.             print "only me left" 
  33.         elif Person.population >=1
  34.             print "there are %s person left" % Person.population 
  35.  
  36. andy = Person("andy"
  37. tom = Person("Tom"
  38. Person.howMany(andy) 

 

你可能感兴趣的:(python,Foundation)