Python notes: class and inheritance, python notes inheritance
- #-*-Coding = UTF-8 -*-
- # The file encoding syntax is: coding [: =]/s * ([-/w.] +)
- # If no encoding is specified, the default value is ASCII.
- # Note that the encoding of physical files must also conform to the encoding specified by the language.
- # More can refer to: http://python.org/dev/peps/pep-0263/
- Class CBase:
- ''' Resource-related class''' # This description can be output through CBase. _ doc _ or class instance. _ doc _
- Counter = 0; # This is a class variable, equivalent to a static variable in c ++, public
- _ Counter = 1; # The variable is considered a private variable with double underscores (_ counter = 1 ).
- # Cannot be accessed outside the class; otherwise, it is considered a public variable.
- # In Python, _ init _ is equivalent to the constructor of C ++,
- # Define the object variable in _ init _, which is equivalent to a common variable in c ++.
- Def _ init _ (self, name ):
- Self. name = name # common object variable, public
- Self. _ private1 = 2 # private common variable, private
- Print ('_ init _ base ')
- # _ Del _ is equivalent to the destructor of c ++
- Def _ del _ (self ):
- Print ('base destruct ')
- # Any class method must have at least one parameter
- # This parameter is equivalent to the this pointer of C ++.
- # This parameter must be the first parameter
- # Conventions generally write self
- Def Help (self ):
- '''''''Help doc''' # doc of the method, reference syntax [class name | instance]. method name. _ doc __
- Print ('--- base help begin ---/N', self. name) # reference common variables here
- Print (CBase. _ counter) # Even if the methods of this class reference their own static variables,
- # You also need to specify the class name or self.
- Print (self. _ private1) # private variables can only be referenced by this class of methods.
- Print ('--- base help end ---/N ')
- Def Test (self ):
- Print ('base test ')
- # Class inheritance
- # Multi-inheritance is supported. The syntax class CC (CA, CB)
- Class CHigh (CBase ):
- Def _ init _ (self ):
- CBase. _ init _ (self, 'high') # manually input the first parameter when calling the construction of the base class
- Print ('_ init _ high ')
- Def _ del _ (self ):
- # CBase. _ del _ (self), The destructor of the base class cannot be called.
- # The destructor of the base class will not be automatically called.
- Print ('high destruct ')
- # The method of the inherited class overwrites the method of the same name of the base class.
- Def Test (self ):
- Print ('high test ')
- # Python does not have the concept of method overloading.
- # The Last defined method in the source file will overwrite the previous method with the same name.
- # The second parameter name must be passed when Test is being called.
- # The preceding Test method with a parameter is overwritten.
- Def Test (self, name ):
- Print ('high Test have arg: ', name)
- Rh = CHigh ()
- # Rh = CBase ('hello ')
- Rh. Help ()
- Rh. Test ();
- Print (CBase. counter)
- Print (rh. name)
Who can use the popular method to inherit the python class? Tell the younger brother about the inheritance of the class.
A general example is:
There is already a class called bird
Its flying methods/functions
Then you implement another sparrow class.
If no class is inherited
Then you need:
First implement the Flying Method
Then other methods (and attributes) specific to Sparrow are implemented separately)
This method is similar to the method of flying. It is obvious that it is a common method used by all species of birds.
Therefore, to avoid this, every other bird and every other bird must implement this method separately.
Therefore, we get a base class, that is, the basic class, the main class.
It implements some common things that everyone shares.
Including many methods and many attributes
Then the other child classes
After this base class is integrated
You don't have to repeat and reimplement the basic methods and attributes.
You only need to implement the unique features of your class.
I don't know if you understand it.
For more information, see my summary:
[Arrangement] basic object-oriented knowledge: Class, Object, and Instance)
(No post address is provided here. You can find the post address by searching the post title on google)
In python, how does one call the methods of the base class in the inheritance class?
Where is the _ init _ constructor. What do you want to call the parent class method. Take Class --..
Give you a comprehensive
Class Q (object ):
Def _ init _ (self ):
Self. x = False
Self. y = True
>>> Q = Q ()
>>> Class W (Q ):
Def _ init _ (self ):
# In this way, the x and y attributes of the parent class Q are inherited.
Q. _ init _ (self)
>>> W = W ()
>>> Print w. x, w. y