Python面向对象的编程

来源:互联网 发布:老七贸易知乎 编辑:程序博客网 时间:2024/05/16 05:17

转自:http://blog.csdn.net/delphiwcdj/article/details/5732908

在Python中,即便是整数也被作为对象 (属于int类)。这和C++把整数作为类型是不同的 。通过help(int) 可以了解更多这个int类的信息。

[注意] 
[1] 使用类名后跟一对圆括号 来创建一个对象/实例。
[2] Python中的self 等价于C++中的this指针 。
[3] __init__方法 类似于C++中的constructor 。
[4] __del__方法 类似于C++中的destructor 。
[5]  是属于一个对象或类的变量。域有两种类型 :实例变量(对象的变量 )和类变量(类的变量 )
[6] 方法 是属于类的函数。
[7] 域和方法统称属性 。
[8] __del__方法在对象消逝的时候调用,并把对象所占的内存返回给系统。
[9] Python中所有的类成员都是公共的 。
[10] __privatevar的双下划线前缀 命名方式为私有变量 。但是,惯例是:使用单下划线前缀 表示私有变量。

  1. #! /usr/bin/python  
  2. # Filename: objvar.py  
  3. # 2010-7-13 wcdj  
  4. class Person:  
  5.         '''''Represents a person.'''  
  6.         population=0  
  7.         def __init__(self,name):  
  8.                 '''''Initializes the person's data.'''  
  9.                 self.name=name  
  10.                 print '(Initializing %s)' % self.name  
  11.                 # When this person is created, he/she  
  12.                 # adds to the population  
  13.                 Person.population += 1  
  14.         def __del__(self):  
  15.                 '''''I am dying'''  
  16.                 print '%s says bye.' % self.name  
  17.                 Person.population -= 1  
  18.                 if Person.population==0:  
  19.                         print 'I am the last one.'  
  20.                 else:  
  21.                         print 'there are still %d people left.' % Person.population  
  22.         def sayHi(self):  
  23.                 '''''Greeting by the person 
  24.                 Really, that's all it does.'''  
  25.                 print 'Hi, my name is %s.' % self.name  
  26.         def howMany(self):  
  27.                 '''''Prints the current population.'''  
  28.                 if Person.population==1:  
  29.                         print 'I am the only person here.'  
  30.                 else:  
  31.                         print 'We have %d persons here.' % Person.population  
  32. print '-------------------'  
  33. print 'Person: ',Person.__doc__  
  34. print 'Person.__init__:',Person.__init__.__doc__  
  35. print 'Person.__del__:',Person.__del__.__doc__  
  36. print 'Person.sayHi:',Person.sayHi.__doc__  
  37. print 'Person.howMany:',Person.howMany.__doc__  
  38. print '-------------------'  
  39.                           
  40.    
  41. def main():    
  42.         gerry=Person('Gerry Young')  
  43.         gerry.sayHi()  
  44.         gerry.howMany()  
  45.         w=Person('wcdj')  
  46.         w.sayHi()  
  47.         w.howMany()  
  48. if __name__ == '__main__':  
  49.         main()  
  50. #########  
  51. # output  
  52. #########  
  53. >>>   
  54. -------------------  
  55. Person:  Represents a person.  
  56. Person.__init__: Initializes the person's data.  
  57. Person.__del__: I am dying  
  58. Person.sayHi: Greeting by the person  
  59.                 Really, that's all it does.  
  60. Person.howMany: Prints the current population.  
  61. -------------------  
  62. (Initializing Gerry Young)  
  63. Hi, my name is Gerry Young.  
  64. I am the only person here.  
  65. (Initializing wcdj)  
  66. Hi, my name is wcdj.  
  67. We have 2 persons here.  
  68. Gerry Young says bye.  
  69. there are still 1 people left.  
  70. wcdj says bye.  
  71. I am the last one.  
  72. >>>   

 

关于上述代码的问题讨论可见:

[1] http://topic.csdn.net/u/20100714/14/5e94e0c6-b958-4469-b116-76857f72ae15.html?seed=252010780&r=67002597#r_67002597

[2] http://topic.csdn.net/u/20100617/00/55e2d94b-1dc7-40ee-ad15-18cef7fba89b.html