Chinaunix首页 | 论坛 | 博客
  • 博客访问: 13571
  • 博文数量: 11
  • 博客积分: 10
  • 博客等级: 民兵
  • 技术积分: 125
  • 用 户 组: 普通用户
  • 注册时间: 2011-11-08 19:49
文章分类
文章存档

2014年(11)

我的朋友
最近访客

分类: Python/Ruby

2014-09-05 14:22:26

1. 类函数/实例函数(class method & instance method),类变量/实例变量(class variable & instance variable):
   根据不同的环境中,以上叫法可能有所不同

    在上面程序中只涉及到了实例函数和实例变量。
    1. 实例函数。(实例方法,instance function,instance method)
        在class定义中定义的函数一般是实例函数,即实例的函数。如上面BinaryTree中定义的函数均为实例函数。实例函数的第一个形参必然是self,代表class创建的instance。实例函数不管是在class里面还是在class外面都需要使用object.instance_method()来调用(在class中,self即表示该class的实例,因此使用self.instance_method()调用)。一般如果实例函数不允许在class外面访问(即调用,使用)的话,在变量名或函数名前面加双下划线("__")。
    2. 实例变量。(实例属性,instance variable,instance attribute)
       在class定义中,使用self.variable定义的变量即为实例变量。如上面BinaryTree中self.value,self.left,self.right,self.parent均为实例变量。一般如果实例变量不允许在class外面访问(即调用,使用)的话,在变量名或函数名前面加双下划线("__"),使其变为私有变量。一般可以单独写非私有实例函数返回实例私有变量的值,从而在class外面访问实例私有变量。

    3. 类变量。(类属性,class variable, class attribute)
       在class里面的最外层定义的变量即为类变量。类变量不用self.variable来定义,区分于实例变量。同时,类变量处于与实例函数/类函数同等的位置(class里最外层定义),区分于局部变量。

    4. 类函数。(类方法,class variable,class attribute)
       即在类中定义的函数。关于类函数的解释(未完待续)。

下面例子说明:
   1. 类变量的作用;
   2. 类变量和实例变量之间的区别;
   3. 类变量如何转化用作实例变量以及此时二者之间的差别;

点击(此处)折叠或打开

  1. class Spam:
  2.     numInstances = 0 # Declare a class attribute
  3.     def __init__(self):
  4.         Spam.numInstances = Spam.numInstances + 1 # count how many instances created
  5.         print("New Spam instance created")
  6.         if Spam.numInstances == 2:
  7.             print("You have created two instance of Spam")
  8.     def printNumInstances(): # class method
  9.         print("Number of instances created: %s" % Spam.numInstances)

  10. obj_1 = Spam()
  11. obj_2 = Spam()
  12. obj_3 = Spam()
  13. # 1. Call of class method
  14. Spam.printNumInstances()
  15. # 2. Access of class variable from outside
  16. Spam.numInstances = 5
  17. # 3. Check if outside access has changed class variable's value
  18. Spam.printNumInstances()
  19. print("Number of instances created: %s" % Spam.numInstances)
  20. # 4. Access of class variable through instance
  21. print("numInstances's value in obj_1: %s" % obj_1.numInstances)
  22. # Access with instance and a copy(reference) of class varialbe are generated as the instance's instance variable
  23. obj_1.numInstances = 10
  24. # Now obj_1's instance has become its independent instance, and has nothing to do with Spam.numInstances, this is like the following process:
  25. # obj_1.numInstances = Spam.numInstances // When you access obj_1.numInstances, you are actually access the same object that Spam.numInstances pointed to (obj_1.numInstances also point to this object)
  26. # obj_1.numInstances = 10 // And now obj_1.numInstances point to another object "10", and has nothing to do with "Spam.numInstances" from now on
  27. print("numInstances's value in obj_1: %s" % obj_1.numInstances)
  28. print("numInstances's value in obj_2: %s" % obj_2.numInstances)
  29. # At this time, obj_2's numInstance is still a reference of Spam.numInstances
  30. obj_2.numInstances = 20
  31. # Now obj_2's instance has become its independent instance, and has nothing to do with Spam.numInstances
  32. print("numInstances's value in obj_2: %s" % obj_2.numInstances)
  33. print("numInstances's value in Spam: %s" % Spam.numInstances)
  1. >>>
  2. New Spam instance created
  3. New Spam instance created
  4. You have created two instance of Spam
  5. New Spam instance created
  6. Number of instances created: 3
  7. Number of instances created: 5
  8. Number of instances created: 5
  9. numInstances's value in obj_1: 5
  10. numInstances's value in obj_1: 10
  11. numInstances's value in obj_2: 5
  12. numInstances's value in obj_2: 20
  13. numInstances
下面例子说明私有变量在类内外的可访问性:
    1. 不管是类变量还是实例变量,当我们设定其为私有变量之后,外部无法直接访问到它。只能通过类函数或实例函数来访问;
   2. 但外部却可以写出如下line 17的句子,此时这个"__numInstances"变量指的是什么呢?相对于类来说它是私有的吗?
  1. class Spam:
  2.     __numInstances = 0 # Declare a class attribute
  3.     def __init__(self):
  4.         Spam.__numInstances = Spam.__numInstances + 1 # count how many instances created
  5.         print("New Spam instance created")
  6.         if Spam.__numInstances == 2:
  7.             print("You have created two instance of Spam")
  8.     def printNumInstances(): # class method
  9.         print("Number of instances created: %s" % Spam.__numInstances)

  10. obj_1 = Spam()
  11. obj_2 = Spam()
  12. obj_3 = Spam()
  13. # 1. Call of class method
  14. Spam.printNumInstances()
  15. # 2. Access of class variable from outside
  16. Spam.__numInstances = 5
  17. # 3. Check if outside access has changed class variable's value
  18. Spam.printNumInstances()
  19. print("After outside change, number of instances created: %s" % Spam.__numInstances)
  20. # 4. Access of class variable through instance
  21. print("numInstances's value in obj_1: %s" % obj_1.__numInstances)
  22. obj_1.__numInstances = 10
  23. print("numInstances's value in obj_1: %s" % obj_1.__numInstances)
  24. print("numInstances's value in obj_2: %s" % obj_2.__numInstances)
  25. print("numInstances
  1. >>>
  2. New Spam instance created
  3. New Spam instance created
  4. You have created two instance of Spam
  5. New Spam instance created
  6. Number of instances created: 3
  7. Number of instances created: 3
  8. After outside change, number of instances created: 5
  9. numInstances's value in obj_1: 5
  10. numInstances's value in obj_1: 10
  11. numInstances's value in obj_2: 5
  12. numInstances's value in Spam: 5
  13. >>>





阅读(933) | 评论(0) | 转发(0) |
0

上一篇:没有了

下一篇:DFS实现SCC算法的证明

给主人留下些什么吧!~~