stay Python in , If there is a parent-child inheritance relationship between the two classes , Even if there are no properties and methods in the subclass , At this point, create a subclass object , Then this subclass object will have the right to use all properties and methods in the parent class , That's what it is Python The concept of inheritance in .
stay Python Testing inheritance , We usually extract one kind of things , With the same characteristics, attributes and behaviors into a class , We call it the father class , It can also be called superclass 、 Base class .
Any class created by inheritance , We call it subclass or derived class , You can directly inherit the properties and methods in the parent class .
Python Object oriented inheritance refers to the relationship between multiple classes , That is, the child class inherits all the properties and methods of the parent class by default , As follows :
# Inherit : The subclass inherits all the properties and methods of the parent class by default
# 1. Define parent class Parent
class Parent(object):
def __init__(self):
self.num = 1
def info_print(self):
print(self.num)
# 2. Defining subclasses Child Inherited parent class Parent
class Child(Parent):
pass
# establish Child Class instance , Call the parent class Parent Methods and properties in
ch = Child()
print(ch.num) # 1
ch.info_print() # 1
explain : stay Python in , All classes inherit by default
object
class ,object
A class is a top-level class or a base class , Other subclasses areobject
The derived class .
Single inheritance : A parent class is inherited by only one subclass , It's called single inheritance .
The main line of the story : A pancake and fruit teacher , I've been in the pancake industry for many years , Developed a set of exquisite techniques for spreading pancakes and fruits . Master wants to teach this technique to his only and most proud apprentice .
# 1. Master class , Properties and methods
class Master(object):
def __init__(self):
self.gongfu = '[ Old recipe for pancakes and fruit ]'
def make_cake(self):
print(f' Application {self.gongfu} Make pancakes and fruit ')
# 2. Define apprentice class , Inherit master class
class Prentice(Master):
pass
# 3. Create objects tudi
tudi = Prentice()
# 4. Object access instance properties
print(tudi.gongfu) # [ Old recipe for pancakes and fruit ]
# 5. Object calls instance methods
tudi.make_cake() # Application [ Old recipe for pancakes and fruit ] Make pancakes and fruit
The story advances : The apprentice is a good student who loves to learn , I want to learn more about pancake fruit technology , So I searched for college pancake fruit , Decided to learn more pancake fruit technology in the College .
The so-called multiple inheritance means that a class inherits more than one parent class at the same time .
# 1. Create master class , Properties and methods
class Master(object):
def __init__(self):
self.gongfu = '[ Old recipe for pancakes and fruit ]'
def make_cake(self):
print(f' Application {self.gongfu} Make pancakes and fruit ')
# 2. Create an academic class Properties and methods
class School(object):
def __init__(self):
self.gongfu = '[ College pancake fruit recipe ]'
def make_cake(self):
print(f' Application {self.gongfu} Make pancakes and fruit ')
# 2. Define apprentice class , Inherit master class and Master class
class Prentice(School, Master):
pass
# 3. Create objects tudi
tudi = Prentice()
# 4. Object access instance properties
print(tudi.gongfu)
# 5. Object calls instance methods
tudi.make_cake()
"""
Output content :
[ College pancake fruit recipe ]
Application [ College pancake fruit recipe ] Make pancakes and fruit
"""
summary :
class Prentice(School, Master):
Catalog 1.Python Introduction to development environment 2.Python The classification of interpreters 3. download Python Interpreter 4. install Python Interpreter 5.Python Interpreter validation 1.Python Introduction to development environment So-called " Desire for work ...
Catalog 1.Python The interaction mode of 2.IDLE Instructions for use of tools 3.Sublime3 Installation and configuration of tools (1)Sublime3 Installation (2)Sublime3 Configuration of 4. Use Sublime Write and debug Pytho ...
Catalog 1.Pycharm download 2.Pycharm install 3.PyCharm Interface is introduced 4. Basic use (1) newly build Python project (2) To write Python Code (3) Execute the code to see the results (4) Set up PyCharm ...
Catalog 1. The concept of sequence 2. Subscript description of string 3. Slice description of string 1. The concept of sequence Sequence sequence yes Python The most basic data structure in . It refers to a continuous memory space that can hold multiple values , These values are arranged in a certain order , Accessible ...
Catalog 1.lambda Application scenarios of 2.lambda grammar 3. Quick start 4. Example : Calculation a + b 5.lambda The parametric form of 6.lambda Application lambda The main function of expressions is to simplify the code . Anonymous functions ...
Catalog 1. Abnormal propagation 2. How to handle exceptions 1. Abnormal propagation When an exception occurs in a function , If an exception is handled in a function , Then the exception will not continue to spread . If the exception is not handled in the function , The exception will continue to propagate to the function caller . If the function is called ...
Catalog 1. What is computer language 2. Compiler language and interpretive language in high level language (1) Compiler language (2) Explanatory language (3) Compiled language and interpreted language execution process 3. Knowledge expansion : 4. About Python 1. What is computer language ...
Catalog 1. The function of annotation 2. Classification of notes Single-line comments Multiline comment 3. Notes 4. When do I need to use comments 5. summary Tips : Finished the preparatory work ahead , The following article begins to introduce Python The basic grammar of . Python ...
Catalog 1. Definition of variables 2.Python Variable description 3.Python Define variables in (1) Definition grammar (2) Identifier definition rules (3) Built in keywords (4) Identifier naming habits 4. Using variables 1. Definition of variables In the program , The data are all ...
Catalog 1. Data type introduction 2. Numerical type (Number) 3. Boolean type (bool) 4.None( Null value ) 5. Constant 6. character string (String) 1. Data type introduction (1) What is data type In the life , What we use everyday ...
mount vhd yes win7 A very special function ,xp Can not support , Some server version systems image 2008.2008R2 These may also support , It's just not tested . Preparation in advance : Win7 wim Image file Imagex.exe ...
Third party component :Mysql.Data.dll explain : Go to the official website to download Mysql.Data.dll, Then add a reference to the component in the project , Enter... In the code page using Mysql.Data.MysqlClient, We can ...
First statement , I'm just a programmer , Not professional DBA, The following article is written from the process of solving a problem , Instead of giving you the right results in the first place , If there is something wrong in the text , Please correct the database , So that I can better handle this business . ...
The golden rule Always follow the same code , No matter how many people are involved in the same project , Make sure that every line of code is written by the same person . grammar : 1. Use two spaces instead of tabs (tab);2. Nested elements should be indented once ( Two spaces ); 3. Yes ...
Maximal continuous subsequence Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Sub ...
From the ordinary Web Page upload file is very easy. Just need to be in form The label is on enctype="multipart/form-data" Can , The rest of the work will be left to the browser to complete the data collection and send Http ...
MAIC 2016 The second mobile app (APP) The Innovation Conference will be held in 2016 year 12 Held in Shanghai in May !MAIC The quality of each session is higher and higher , The larger . This year, it's the same , This year MAIC More than 2000 people . The conference is a professional meeting , Innovation and Application Exhibition ...
introduction ps: Xiaobai can have a look , Don't hit the great God ~ Used to ztree My friends all know ,ztree Powerful , Functionally , In pursuit of “ Button everywhere ” Experience , But easy to use but not good-looking . Maybe a friend said :“ I can only see this tree by myself , Enough is enough. ” ...
Studying recently Linux virtual machine , Here are some instructions I used in the process of learning virtual machine , And its role . pwd-> List the current directory path ls-> List the current directory cd-> Change the directory mkdir-> ...
Blog for the first time , I don't know how to write it . With a learning heart , Also hope to come back after a period of time to see their own code , Can let oneself have a kind of idea that cannot bear to look directly at *-* Let's start with the picture above ~ This is the home page , The main thing is to show all the list pages , You can lose ...