教为学:Python学习之路(六):类

教为学:Python学习之路(六):类

类与对象

通俗点说类是定义,对象是实体。

简单点说人是类,高鹏我是对象。

属性

属性有实例属性和类属性之分。

先上一段代码看看:

  1. class Fruit:
  2.     price = 0
  3.  
  4.     def __init__(self):
  5.         self.color='red'
  6.         zone="china"
  7.  
  8. if __name__=="__main__":
  9.      print "Fruit price:%d"%Fruit.price
  10.      apple = Fruit()
  11.      print "apple color:%s"%apple.color
  12.      print "apple price:%d"%apple.price
  13.      banane = Fruit()
  14.      print "banane color:%s"%banane.color
  15.      print "banane price:%d"%banane.price
  16.  
  17.      Fruit.color="yellow"
  18.      Fruit.price=50
  19.  
  20.      print "apple color:%s"%apple.color
  21.      print "apple price:%d"%apple.price
  22.      print "banane color:%s"%banane.color
  23.      print "banane price:%d"%banane.price
  24. #结果
  25. Fruit price:0
  26. apple color:red
  27. apple price:0
  28. banane color:red
  29. banane price:0
  30. apple color:red
  31. apple price:50
  32. banane color:red
  33. banane price:50

通过类可以直接修改所有类的类属性,却不可以修改实例属性。

修改17、18行上。

  1. class Fruit:
  2.     price = 0
  3.  
  4.     def __init__(self):
  5.         self.color='red'
  6.         zone="china"
  7.  
  8. if __name__=="__main__":
  9.      print "Fruit price:%d"%Fruit.price
  10.      apple = Fruit()
  11.      print "apple color:%s"%apple.color
  12.      print "apple price:%d"%apple.price
  13.      banane = Fruit()
  14.      print "banane color:%s"%banane.color
  15.      print "banane price:%d"%banane.price
  16.  
  17.      apple.color="yellow"
  18.      apple.price=50
  19.  
  20.      print "apple color:%s"%apple.color
  21.      print "apple price:%d"%apple.price
  22.      print "banane color:%s"%banane.color
  23.      print "banane price:%d"%banane.price
  24. #结果
  25. Fruit price:0
  26. apple color:red
  27. apple price:0
  28. banane color:red
  29. banane price:0
  30. apple color:yellow
  31. apple price:50
  32. banane color:red
  33. banane price:0

对实例变量修改只影响本身实例变量和本实例的类属性。

再谈一句很搞笑的。

  1. class Fruit:
  2.     price = 0
  3.  
  4.     def __init__(self):
  5.         self.color='red'
  6.         zone="china"
  7.  
  8. if __name__=="__main__":
  9.      print "Fruit price:%d"%Fruit.price
  10.      apple = Fruit()
  11.      print "apple price:%d"%apple.price
  12.      banane = Fruit()
  13.      print "banane price:%d"%banane.price
  14.  
  15.      apple.price=30
  16.      Fruit.price=50
  17.  
  18.      print "apple price:%d"%apple.price
  19.      print "banane price:%d"%banane.price
  20. #结果
  21. Fruit price:0
  22. apple price:0
  23. banane price:0
  24. apple price:30
  25. banane price:50

如果实例修改了类属性就会脱离类属性。

类的方法

  1. class Fruit:
  2.     price = 0
  3.  
  4.     def __init__(self):
  5.         self.color='red'
  6.         zone="china"
  7.     def printColor(self):
  8.         print "color:"+self.color
  9.     def printPrice(self):
  10.         print "price:%d"%self.price
  11.     @staticmethod
  12.     def printStatic():
  13.         print "static"
  14.  
  15.  
  16.  
  17. if __name__=="__main__":
  18.     Fruit.printStatic()
  19.     apple = Fruit()
  20.     apple.printStatic()
  21.     apple.printPrice()
  22.     apple.printColor()

普通方法自带self参数,可以通过self访问实例属性和类属性。而静态方法不自带self参数,也不能通过self访问实例参数和类参数。

  1. class Fruit:
  2.     price = 0
  3.     @staticmethod
  4.     def printStatic():
  5.         print Fruit.price
  6.  
  7. if __name__=="__main__":
  8.     Fruit.printStatic()

它只能通过类直接访问。

构造函数和析构函数

  1. class Fruit:
  2.     count=0
  3.     def __init__(self):
  4.         print "我被调用了"
  5.  
  6.     def __del__(self):
  7.         print "我也不幸被调用了"
  8.  
  9. if __name__=="__main__":
  10.     apple = Fruit()
  11. #结果
  12. 我被调用了
  13. 我也不幸被调用了
  14.  
  15.  
  16. class Fruit:
  17.     count=0
  18.     def __init__(self):
  19.         print "我被调用了"
  20.  
  21.     def __del__(self):
  22.         print "我也不幸被调用了"
  23.  
  24. if __name__=="__main__":
  25.     apple = Fruit()
  26.     Fruit.count
  27. #结果
  28. 我被调用了
  29. 我也不幸被调用了

还类初始化和销毁的时候被调用。

而类被调用的时候不会被自动调用。

总结

这次对类做了一些基本的介绍。下一次我们谈谈继承、多态。

 

 

 

posted @ 2013-06-08 18:00  教为学  阅读(1076)  评论(3编辑  收藏  举报