I. Class Definition:
Copy codeThe Code is as follows:
Class <class Name>:
<Statement>
After the class is instantiated, you can use its attributes. In fact, after creating a class, you can access its attributes through the class name. If you directly use the class name to modify its attributes, it will directly affect the instantiated object.
Private attributes of the class:
_ Private_attrs starts with an underscore and declares that this attribute is private and cannot be used outside the class or accessed directly. Self. _ private_attrs used in internal methods of the class
Class Method
Inside the class, you can use the def keyword to define a method for the class. Unlike the general function definition, the class method must contain the self parameter and be the first parameter.
Private Class Method
_ Private_method starts with two underscores. It is declared as a private method and cannot be called outside the class. Call slef. _ private_methods inside the class
Class proprietary method:
_ Init _ constructor called when an object is generated
_ Del _ destructor used to release objects
_ Repr _ print, convert
_ Setitem _ assign values based on the index
_ Getitem _ obtain the value based on the index
_ Len _ Get the length
_ Cmp _ comparison Calculation
_ Call _ function call
_ Add _ addition operation
_ Sub _ Subtraction
_ Mul _ Multiplication operation
_ Div _ division operation
_ Mod _ remainder operation
_ Pow _ caller
Copy codeThe Code is as follows:
# Class definition
Class people:
# Define basic attributes
Name =''
Age = 0
# Define private attributes. Private attributes cannot be directly accessed outside the class.
_ Weight = 0
# Define the constructor
Def _ init _ (self, n, a, w ):
Self. name = n
Self. age =
Self. _ weight = w
Def speak (self ):
Print ("% s is speaking: I am % d years old" % (self. name, self. age ))
P = people ('Tom ', 10, 30)
P. speak ()
Ii. Inheritance class definition:
1. Single inheritance
Copy codeThe Code is as follows:
Class <class Name> (parent class name)
<Statement>
Copy codeThe Code is as follows:
Class childbook (book)
Age = 10
Copy codeThe Code is as follows:
# Single inheritance example
Class student (people ):
Grade =''
Def _ init _ (self, n, a, w, g ):
# Call the structure of the parent class
People. _ init _ (self, n, a, w)
Self. grade = g
# Override the parent class Method
Def speak (self ):
Print ("% s is speaking: I am % d years old, and I am in grade % d" % (self. name, self. age, self. grade ))
S = student ('ken', 20, 60, 3)
S. speak ()
2. Multi-inheritance of Classes
Copy codeThe Code is as follows:
Class name (parent class 1, parent class 2,..., parent class n)
<Statement 1>
Note the sequence of the parent class in parentheses. If the parent class has the same method name but is not specified in the subclass, search for python from left to right, that is, if the method is not found in the subclass, you can check from left to right whether the parent class contains the method.
Copy codeThe Code is as follows:
# Another class, preparations before multi-Inheritance
Class speaker ():
Topic =''
Name =''
Def _ init _ (self, n, t ):
Self. name = n
Self. topic = t
Def speak (self ):
Print ("I am % s, I am a speaker! My topic is % s "% (self. name, self. topic ))
# Multi-Inheritance
Class sample (speaker, student ):
A =''
Def _ init _ (self, n, a, w, g, t ):
Student. _ init _ (self, n, a, w, g)
Speaker. _ init _ (self, n, t)
Test = sample ("Tim", 25, 80, 4, "Python ")
Test. speak () # The method name is the same. By default, the method of the parent class is called in brackets.