程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式師世界 >> 編程語言 >  >> 更多編程語言 >> Python

No action, no intention Python object-oriented-53. Introduction to encapsulation in Python

編輯:Python
Catalog
  • 1、 Concept of inheritance
  • 2、 Benefits of inheritance
  • 3、 Inheritance experience
  • 4、 Single inheritance
  • 5、 Multiple inheritance

1、 Concept of inheritance

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 .

2、 Benefits of inheritance

  • The biggest function of inheritance is to simplify the code , Improved code reusability , Improve the efficiency of software development .
  • The emergence of inheritance creates a relationship between classes , It provides the premise of polymorphism .

3、 Inheritance experience

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 are object The derived class .

4、 Single inheritance

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

5、 Multiple inheritance

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 :

  • stay Python Multiple inheritance is supported in , That is, we can specify multiple parent classes for a class at the same time , Such as :
    class Prentice(School, Master):
  • multiple inheritance , Will make the subclass have more than one parent at the same time , And will get all the properties and methods in the parent class .
  • If there are methods with the same name in more than one parent class , It will now look in the first parent class for , Then find the second , And then find a third , And so on .

    ( Nagging in detail , Find the first class , Then find the parent class of this class , If you don't start looking for the second class , Then if the second class has a parent class , Find the parent of the second class , And so on , If there is a duplicate parent class , The class I found before , Do not repeat the search .)

    That is, if a class inherits multiple parent classes , Inherit the properties and methods with the same name of the first parent class first .
  • There is no special case in development , Multiple inheritance should be avoided as much as possible , Because multiple inheritance makes our code too complex .

『 No action, no heart 』Python object-oriented — 53、 Yes Python More related articles about the introduction of encapsulation in

  1. 『 No action, no heart 』Python Basics — 3、 build Python development environment

    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 ...

  2. 『 No action, no heart 』Python Basics — 4、Python Common debugging tools for code

    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 ...

  3. 『 No action, no heart 』Python Basics — 5、Python Installation and use of development tools

    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 ...

  4. 『 No action, no heart 』Python Basics — 16、Python Subscript and slice of string of sequence

    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 ...

  5. 『 No action, no heart 』Python function — 34、lambda expression

    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 ...

  6. 『 No action, no heart 』Python function — 39、Python Propagation of exceptions in

    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 ...

  7. 『 No action, no heart 』Python Basics — 2、 The difference between compiled and interpreted languages

    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 ...

  8. 『 No action, no heart 』Python Basics — 6、Python Notes

    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 ...

  9. 『 No action, no heart 』Python Basics — 7、Python The variable of

    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 ...

  10. 『 No action, no heart 』Python Basics — 8、Python Data types in ( The number 、 Boolean 、 character string )

    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 ...

Random recommendation

  1. win7 mount VHD file , Simulate the coexistence of multiple systems

    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 ...

  2. C# Connection operation mysql example

    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 ...

  3. How to be in SQLServer We process 430 million records a day

    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 . ...

  4. How to write specifications , flexible , Stable , High quality HTML and css Code

    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 ...

  5. Maximal continuous subsequence (HDU 1231 DP)

    Maximal continuous subsequence Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Sub ...

  6. Java Use HttpURLConnection Upload files

    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 ...

  7. Talking about MAIC 2016 The second mobile app (APP) Innovation Conference

    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 ...

  8. Ztree Revision - Font icons - fontAwesome

    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. ” ...

  9. Linux Instruction set

    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-> ...

  10. be based on vue 、vue-router 、firebase Of todolist Small projects

    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 ...


  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式師世界 All Rights Reserved