Python Automation Development Learning "Seventh Day"

Source: Internet
Author: User

Introduction to Object Orientation

    • A. Process-oriented: Write code from top to bottom based on business logic.
    • B. Functional programming: Encapsulate a function code into a function, and you will not need to write it again in the future, just call the function.
    • C. Object-oriented: classify and encapsulate functions.

Note: Both Java and C # support object-oriented programming only, and Python is more flexible in that it supports object-oriented and functional programming.

Creating Classes and objects
Object-oriented programming is a programming method that is implemented by using "classes" and "objects", so object-oriented is actually used for "class" and "object".

    • A. A class is a template, and a template can contain multiple functions that implement some function.
    • B. An object is an instance created from a template that can execute functions in a class through an instance object.
1 class Foo:     # class keyword, which means to create a class whose name is Foo2def Bar (self):  #  def keyword, Create a function (also called a method), bar is the function name, self for special parameters must have!!! Self generally points to object 3pass      #  function Contents 4 obj = Foo ()  #  Create object based on class Foo obj5 obj.bar ()     #  Execute Bar Method      

Object-oriented and functional programming performs a function difference

    • A. Object-oriented: Create the object first, then execute the method (function) from the object
    • B. Function: Direct execution function

# Seemingly functional programming is more simple, the actual scenario is different, the programming method is different, functional programming is used for independent and no common data between functions.

Object-oriented three major features (encapsulation, inheritance, polymorphism)

1. Encapsulation
Encapsulation, encapsulating the content somewhere, and then invoking the content that is encapsulated somewhere.
1): Encapsulate the content somewhere
Example

1 classFoo:#Create Class2     def __init__(Self, name, age):#called a construction method, automatically executes when an object is created from a class, and self represents the object3Self.name =name4Self.age = Age5     defDetail (self):#Create a Detaill method6       Print(Self.name)7       Print(Self.age)

# Process: Create objects from class Foo and automate the __init__ method of the Foo class
Obj1 = Foo (' Wu ', 22) # encloses Wu and 22 respectively in the name and age properties of obj1/self
# process, the constructor is automatically executed each time the object is instantiated
Obj2 = Foo (' Alex ', 22) # wrap Alex and 22 separately into the name and age properties of obe2/self
# Self is a formal parameter, self equals obj1 when executing obj1, and self equals obj2 when executing obj2
2): The encapsulated content is called from somewhere
When the encapsulated content is called, there are two scenarios:

    • A. Calling directly through an object: Obj1.name # Call the Name property of the Obj1 object directly
    • B. Via self indirect Call: Obj2.detail () # Python by default passes obj2 to self, namely: Obj2.detail (OBJ2), method internal Self=obj2

In summary, for object-oriented encapsulation, it is actually using the construction method to encapsulate the content into the object, and then indirectly through the direct or self to obtain the encapsulated content.

2. Inheritance
Inheritance, object-oriented, subclasses can inherit from a parent class, in addition to subclasses and parent classes, which are called derived classes and base classes. It's a different term.
Personal understanding: Subclasses inherit all the methods and constructors of all subclasses without the parent class, and of course, if both subclasses and parent classes have one, the subclass will take precedence.
When a child class runs a method or constructor method of the parent class, self points to the object itself.
1). Single Inheritance

1 #single-Inheritance code format:2 classParent class:3     defParent class method (self):4       Pass5 classChild Class (parent Class):#in parentheses, write the parent class that asks for inheritance, and the subclass inherits the parent class, which has all the methods of the parent class .6     defChild class Method (self):7       Pass8obj = Subclass ()#to create an object of the child class9Obj. Method of parent class ()#to perform a method inherited from a parent class


2). Multiple inheritance # Multiple inheritance code format:
Classes of 1.python can inherit multiple classes, and Java and C # can inherit only one class
2.python classes If you want to inherit more than one class, there are two ways of looking at it, namely depth first and breadth first.

    1. When a class is a classic class, multiple inheritance cases are searched in the depth-first way
    2. When a class is in a new class, multiple inheritance cases will look for the breadth-first method

3. Classic and modern classes, the new class contains more functions, but also the recommended wording, from the wording of the difference between the current class or the parent class inherits the Obejct class,
Then the class is the new class, otherwise it is the classic class.
Example

1 classC1:#C1 is a classic class2     Pass3 classC2 (object):#C2 is a new class4     Pass5 classC3 (C2):#C3 inherits the C2, and so is the new class.6     Pass 7 classC4 (C1):#C4 inherited the C1, and so is the classic class8     Pass9 classC5 (C2,C1):#C5 inherits the C2,C1 two parent class, inherits the parent class of C2 Precedence, inherits sequentiallyTen     Pass

3. polymorphic
Python does not support polymorphism and is not polymorphic, polymorphism is used in strongly typed languages such as Java and C #, while Python advocates "duck type"


Members of the Class (Fields, methods, properties)
1. Fields
Fields include: normal fields and static fields, which differ in usage, the most essential difference being the location in memory where they are stored.
Normal fields belong to: # object
Static fields belong to: # class
# Sample Code:

1 classProvince:2     #static fields3Country ='China'4 5     def __init__(self,name):6       #normal field7Self.name =name8 #direct access to normal fields9obj = Province ('Hebei province')Ten Print(Obj.name) One #direct access to static fields AProvince.country


2. Methods
Methods include: Common methods, static methods, and class methods, three methods in memory belong to the class, the difference is that the invocation method is different.


Python Automation Development Learning "Seventh Day"

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.