[Python] python 类继承演示范例 →→→→→进入此内容的聊天室

来自 , 2021-01-31, 写在 Python, 查看 121 次.
URL http://www.code666.cn/view/3817157c
  1. # a simple example of a class inheritance
  2. # tested with Python24     vegaseat    10aug2005
  3.  
  4. help('object')  # test
  5.  
  6. class Class1(object):
  7.     """
  8.    Class1 inherits the most basic container class object (just a place holder)
  9.    this is the newer class writing convention, adding (object) is "still" optional
  10.    """
  11.    
  12.     k = 7
  13.    
  14.     def __init__(self, color='green'):
  15.         """
  16.        Special method __init__() is called first (acts as Constructor).
  17.        It brings in data from outside the class like the variable color.
  18.        (in this case color is also set to a default value of green)
  19.        The first parameter of any method/function in the class is always self,
  20.        the name self is used by convention.  Assigning color to self.color allows it
  21.        to be passed to all methods within the class.  Think of self as a carrier,
  22.        or if you want impress folks call it target instance object.
  23.        The variable k is assigned a value in the class, but outside of the methods.
  24.        You can access k in a method using self.k
  25.        """
  26.         self.color = color
  27.    
  28.     def Hello1(self):
  29.         print "Hello from Class1!"
  30.        
  31.     def printColor(self):
  32.         """in this case self allows color to be passed"""
  33.         print "I like the color", self.color
  34.      
  35.     def __localHello(self):
  36.         """
  37.        A variable or function with a double underline prefix and no or max. single
  38.        underline postfix is considered private to the class and is not inherited or
  39.        accessible outside the class.
  40.        """
  41.         print "A hardy Hello only used within the class!"
  42.  
  43.  
  44. class Class2(Class1):
  45.     """
  46.    Class2 inherits Class1 (Class2 is the subclass, Class1 the base or superclass)
  47.    Class1 has to be coded before Class2 for this to work!!!
  48.    Class2 can now use any method of Class1, and even the variable k
  49.    """
  50.  
  51.     def Hello2(self):
  52.         print "Hello from Class2!"
  53.         print self.k, "is my favorite number"
  54.        
  55.    
  56. # the color blue is passed to __init__()
  57. c1 = Class1('blue')
  58.  
  59. # Class2 inherited method __init__() from Class1
  60. # if you used c2 = Class2(), the default color green would be picked
  61. c2 = Class2('red')
  62.  
  63. print '-'*20
  64. print "Class1 says hello:"
  65. c1.Hello1()
  66.  
  67. print '-'*20
  68. print "Class2 says a Class1 hello:"
  69. c2.Hello1()
  70.  
  71. print '-'*20
  72. print "Class2 says its own hello:"
  73. c2.Hello2()
  74.  
  75. print '-'*20
  76. print "Class1 color via __init__():"
  77. c1.printColor()
  78.  
  79. print '-'*20
  80. print "Class2 color via inherited __init__() and printColor():"
  81. c2.printColor()
  82.  
  83. print '-'*20
  84. print "Class1 changes its mind about the color:"
  85. c1 = Class1('yellow')  # same as:  c1.__init__('yellow')
  86. c1.printColor()
  87.  
  88. print '-'*20
  89. print "Wonder what Class2 has to say now:"
  90. c2.printColor()
  91.  
  92. print '-'*20
  93. # this would give an error!  Class1 does not have a method Hello2()
  94. if hasattr(Class1, "Hello2"):
  95.     print c1.Hello2()
  96. else:
  97.     print "Class1 does not contain method Hello2()"
  98.  
  99. # check inheritance
  100. if issubclass(Class2, Class1):
  101.     print "Class2 is a subclass of Class1, or Class2 has inherited Class1"
  102.  
  103. # you can access variable k contained in Class1
  104. print "Variable k from Class1 =", c1.k
  105.  
  106. print '-'*20
  107. # this would give an error!  You cannot access a class private method
  108. if hasattr(Class1, "__localHello()"):
  109.     print c1.__localHello()
  110. else:
  111.     print "No access to Class1 private method __localHello()"
  112. #//python/5751

回复 "python 类继承演示范例"

这儿你可以回复上面这条便签

captcha