Python study note 6 (class inheritance)

Source: Internet
Author: User

Single inheritance
The specific syntax is not mentioned. What I care about is what it inherits
A single inheritance condition is very simple. Static variables of the class, static methods of the class, methods of the instance, member variables of the instance, constructors of the base class, and destructor of the base class can all be inherited from the parent class. Private methods and private variables cannot be inherited. Test Code :
Class student:
'''This test class '''
Name = 'ss'
Age = 13
Def _ init _ (Self ):
Self. Name = 'bb'
Self. ADDR = "Shanghai"
Self. _ love = "man"
Print "init ..."
Def _ del _ (Self ):
Print "dead ..."
Def run (Self ):
Print 'people run'
@ Staticmethod
Def runstatic ():
Print "in static method ..."
Def _ Good (Self ):
Print "good"
Class ministudent (student ):
Pass
# Class
Print ministudent. Name
Print ministudent. Age
Print ministudent. runstatic ()

Print "---- ======================== --------"
# Instance
Mini = ministudent ()
Print Mini. Name
Print Mini. Age
Print Mini. Run ()
Print Mini. ADDR

Multi-Inheritance
In the case of multi-inheritance, the situation is obviously more complex than this, and it will produce the problem of multiple base class override functions. Fortunately, there is no more inheritance in C #, and you can only inherit from multiple interfaces. Because of this, I feel that this kind of multi-inheritance should not be abused, otherwise the code will be obscure. You can see the following example. In the above example, I added another studenta class. Test code:

Class studenta:
Def _ init _ (Self ):
Print 'init-studenta'
Def gocar (Self ):
Print "go car"
Class student:
'''This test class '''
Name = 'ss'
Age = 13
Def _ init _ (Self ):
Self. Name = 'bb'
Self. ADDR = "Shanghai"
Self. _ love = "man"
Print "init ..."
Def _ del _ (Self ):
Print "dead ..."
Def run (Self ):
Print 'people run'
@ Staticmethod
Def runstatic ():
Print "in static method ..."
Def _ Good (Self ):
Print "good"
Class ministudent (student, studenta ):
Pass
# Class

Mini = ministudent ()
Print Mini. Name
Print Mini. Age
Print Mini. Run ()
Print Mini. ADDR

This example can be used now, but if you
Class ministudent (student, studenta)
Modify
Class ministudent (studenta, student)
This code won't work. It tells you that mini. ADDR is not defined.
The studenta constructor overwrites the student function, that is, it only calls the studenta. _ init _ () method. Next we will talk about the overwriting in inheritance.

Overwrite in inheritance
From the above experience, we can get
Python has multiple inheritance, but note the following:
1. The call of the inherited method is related to the declaration sequence of the base class (the method after overwriting is used first ).
2. base classes _ init _ () and _ del _ (). If you want to use them all, please display the call. Otherwise, errors may occur easily.

class studenta:
def _ init _ (Self):
Print 'init-B '
class student:
def _ init _ (Self):
Print 'init-a'
class ministudent (student, studenta ):
def _ init _ (Self):
studenta. _ init _ (Self)
student. _ init _ (Self)
# instance
mini = ministudent ()

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.