The example in this article describes the Python class inheritance usage. Share to everyone for your reference. Specifically as follows:
?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26-27--28 29---30 31--32 33 34 35 36 37 38-39 40 41 42 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 5 86 87 |
Help (' object ') # Test class Class1 (object): "" "Class1 inherits the most basic container class object (just a place Holde R) This is the newer class writing convention, adding (object) is "still" optional "" "K = 7 def __init__ (self, color= ' GRE En '): "" "Special Method __init__ () is called-I (acts as constructor). It brings in data from outside the class like the variable color. (in the case, color is also set to a default value of green) The parameter of any method/function in the class is always self, the name self is used by convention. Assigning color to Self.color allows it to is passed to all methods within the class. "It" is a carrier, or if you want impress folks call it target instance object. The variable k is assigned a value in the class, but outside of the methods. You can access K in a method using the Self.k "" "Self.color = Color def Hello1 (self): print" Hello from class1! "Def Printcol or (self): "" "" "" "" "Allows" "" "" "" Passed "" "" Print "I LiKe the Color ", Self.color def __localhello (self):" "A variable or function with a double underline prefix and no or max. Single underline postfix are considered private to the class and are not inherited or accessible outside the class. "" "" Print "A hardy Hello only used within the class!" class Class2 (CLASS1): "" "Class2 inherits Class1 (Class2 is t He subclass, Class1 the "base or superclass" Class1 has to is coded before to CLASS2!!! Class2 can now do any of Class1, and even the variable K "" Def Hello2 (self): print "Hello to class2!" Print SE LF.K, "is my favorite number" # The color blue is passed to __init__ () C1 = Class1 (' Blue ') # Class2 inherited Metho D __init__ () from Class1 # if you used C2 = Class2 (), the default color green would is picked C2 = Class2 (' red ') print '-' *20 print "Class1 says hello:" C1. Hello1 () print '-' *20 print ' Class2 says a Class1 hello: ' C2. Hello1 () print '-' *20 print ' Class2 says its own hello: C2. Hello2 () print '-' *Print "Class1 color via __init__ ():" C1.printcolor () print '-' *20 print ' Class2 Color via inherited __init__ () and Prin TColor (): "C2.printcolor () print '-' *20 print ' Class1 changes its mind about the color:" C1 = Class1 (' yellow ') # Same as: c1.__init__ (' Yellow ') c1.printcolor () print '-' *20 print ' wonder ' what Class2 has to say now: ' C2.printcolor () print '-' *20 # This would give a error! Class1 does not have a method Hello2 () if Hasattr (Class1, "Hello2"): Print C1. Hello2 () else:print "Class1 does not contain method Hello2 ()" # Check inheritance if Issubclass (Class2, Class1): print "C Lass2 is a subclass of Class1, or Class2 has inherited Class1 "# and can access variable K contained in CLASS1 print" Vari Able k from Class1 = ", c1.k print '-' *20 # This would give a error! Cannot access a class private method if Hasattr (Class1, "__localhello ()"): Print C1.__localhello () else:print "No acc ESS to Class1 Private method __localhello () " |
The results of the operation are as follows:
?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 The |
|
I hope this article will help you with your Python programming.