发牌SO 发表于 2015-4-20 13:17:09

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

教为学:Python学习之路(六):类
类与对象
  通俗点说类是定义,对象是实体。
  简单点说人是类,高鹏我是对象。
属性
  属性有实例属性和类属性之分。
  先上一段代码看看:

[*]
class Fruit:
[*]
    price = 0
[*]
[*]
    def __init__(self):
[*]
      self.color='red'
[*]
      zone="china"
[*]
[*]
if __name__=="__main__":
[*]
   print "Fruit price:%d"%Fruit.price
[*]
   apple = Fruit()
[*]
   print "apple color:%s"%apple.color
[*]
   print "apple price:%d"%apple.price
[*]
   banane = Fruit()
[*]
   print "banane color:%s"%banane.color
[*]
   print "banane price:%d"%banane.price
[*]
[*]
   Fruit.color="yellow"
[*]
   Fruit.price=50
[*]
[*]
   print "apple color:%s"%apple.color
[*]
   print "apple price:%d"%apple.price
[*]
   print "banane color:%s"%banane.color
[*]
   print "banane price:%d"%banane.price
[*]
#结果
[*]
Fruit price:0
[*]
apple color:red
[*]
apple price:0
[*]
banane color:red
[*]
banane price:0
[*]
apple color:red
[*]
apple price:50
[*]
banane color:red
[*]
banane price:50
  通过类可以直接修改所有类的类属性,却不可以修改实例属性。
  修改17、18行上。

[*]
class Fruit:
[*]
    price = 0
[*]
[*]
    def __init__(self):
[*]
      self.color='red'
[*]
      zone="china"
[*]
[*]
if __name__=="__main__":
[*]
   print "Fruit price:%d"%Fruit.price
[*]
   apple = Fruit()
[*]
   print "apple color:%s"%apple.color
[*]
   print "apple price:%d"%apple.price
[*]
   banane = Fruit()
[*]
   print "banane color:%s"%banane.color
[*]
   print "banane price:%d"%banane.price
[*]
[*]
   apple.color="yellow"
[*]
   apple.price=50
[*]
[*]
   print "apple color:%s"%apple.color
[*]
   print "apple price:%d"%apple.price
[*]
   print "banane color:%s"%banane.color
[*]
   print "banane price:%d"%banane.price
[*]
#结果
[*]
Fruit price:0
[*]
apple color:red
[*]
apple price:0
[*]
banane color:red
[*]
banane price:0
[*]
apple color:yellow
[*]
apple price:50
[*]
banane color:red
[*]
banane price:0
  对实例变量修改只影响本身实例变量和本实例的类属性。
  再谈一句很搞笑的。

[*]
class Fruit:
[*]
    price = 0
[*]
[*]
    def __init__(self):
[*]
      self.color='red'
[*]
      zone="china"
[*]
[*]
if __name__=="__main__":
[*]
   print "Fruit price:%d"%Fruit.price
[*]
   apple = Fruit()
[*]
   print "apple price:%d"%apple.price
[*]
   banane = Fruit()
[*]
   print "banane price:%d"%banane.price
[*]
[*]
   apple.price=30
[*]
   Fruit.price=50
[*]
[*]
   print "apple price:%d"%apple.price
[*]
   print "banane price:%d"%banane.price
[*]
#结果
[*]
Fruit price:0
[*]
apple price:0
[*]
banane price:0
[*]
apple price:30
[*]
banane price:50
  如果实例修改了类属性就会脱离类属性。
类的方法

[*]
class Fruit:
[*]
    price = 0
[*]
[*]
    def __init__(self):
[*]
      self.color='red'
[*]
      zone="china"
[*]
    def printColor(self):
[*]
      print "color:"+self.color
[*]
    def printPrice(self):
[*]
      print "price:%d"%self.price
[*]
    @staticmethod
[*]
    def printStatic():
[*]
      print "static"
[*]
[*]
[*]
[*]
if __name__=="__main__":
[*]
    Fruit.printStatic()
[*]
    apple = Fruit()
[*]
    apple.printStatic()
[*]
    apple.printPrice()
[*]
    apple.printColor()
  普通方法自带self参数,可以通过self访问实例属性和类属性。而静态方法不自带self参数,也不能通过self访问实例参数和类参数。

[*]
class Fruit:
[*]
    price = 0
[*]
    @staticmethod
[*]
    def printStatic():
[*]
      print Fruit.price
[*]
[*]
if __name__=="__main__":
[*]
    Fruit.printStatic()
  它只能通过类直接访问。
构造函数和析构函数

[*]
class Fruit:
[*]
    count=0
[*]
    def __init__(self):
[*]
      print "我被调用了"
[*]
[*]
    def __del__(self):
[*]
      print "我也不幸被调用了"
[*]
[*]
if __name__=="__main__":
[*]
    apple = Fruit()
[*]
#结果
[*]
我被调用了
[*]
我也不幸被调用了
[*]
[*]
[*]
class Fruit:
[*]
    count=0
[*]
    def __init__(self):
[*]
      print "我被调用了"
[*]
[*]
    def __del__(self):
[*]
      print "我也不幸被调用了"
[*]
[*]
if __name__=="__main__":
[*]
    apple = Fruit()
[*]
    Fruit.count
[*]
#结果
[*]
我被调用了
[*]
我也不幸被调用了
  还类初始化和销毁的时候被调用。
  而类被调用的时候不会被自动调用。
总结
  这次对类做了一些基本的介绍。下一次我们谈谈继承、多态。
  
  
  
页: [1]
查看完整版本: 教为学:Python学习之路(六):类