On object-oriented

Source: Internet
Author: User

Process oriented: Write base code from top to bottom according to business logic

Function: A function code is encapsulated in a function, it will not need to be repeated later, only the function can be called

Object-oriented: classify and encapsulate functions to make development "faster, better and stronger ..."

Process-oriented programming is most easily accepted by beginners, it often uses a long code to achieve the specified function, the most common operation in the development process is to paste the copy, that is: Copy the previously implemented code block to the current function.

While True:    if CPU utilization > 90%:        #发送邮件提醒        Connect mailbox server        Send mail        off connection     if hard disk use space > 90%:        #发送邮件提醒        Connect mailbox server        Send mail        close connection     If memory consumption > 80%:        #发送邮件提醒        Connect mailbox server        Send mail        close connection

Over time, functional programming has been used to enhance the reusability and readability of code, and it has become:

def send mail (content)    #发送邮件提醒    connect a mailbox server to    send mail    off connection while True:     if CPU utilization > 90%:        send mail (' CPU alert ')     if HDD uses space > 90%:        send mail (' HDD alert ')     if memory consumption > 80%:        
creating classes and Objects

Object-oriented programming is a way of programming, which requires "classes" and "objects" to be implemented, so object-oriented programming is actually the use of "classes" and "objects".

A class is a template that can contain multiple functions and functions in a template.

Objects are instances created from templates that can execute functions in a class through an instance object

    • Class is a keyword that indicates that classes
    • Create the object, and then add parentheses to the class name
# Create class foo:         def Bar (self):        print ' bar '     def Hello (self, name):        print ' I am%s '%name # creates an object from class Foo Objobj = Foo () obj. Bar ()            #执行Bar方法obj. Hello (' Wupeiqi ') #执行Hello方法

  

Observing the above-mentioned answers is affirmative, then not absolute, and the different scenarios are different for their programming styles.

Summary: Functional application Scenarios--independent and no shared data between functions

The three major characteristics of object-oriented are: encapsulation, inheritance and polymorphism. first, the package

Encapsulation, as the name implies, encapsulates the content somewhere and then calls the content that is encapsulated somewhere.

Therefore, when using object-oriented encapsulation features, you need:

    • Encapsulate content somewhere
    • To invoke the encapsulated content from somewhere

First step: encapsulate the content somewhere

Self is a formal parameter, when executing obj1 = Foo (' Wupeiqi ', 18), self equals obj1

When executing obj2 = Foo (' Alex ', 78), self equals obj2

So, the content is actually encapsulated in objects Obj1 and OBJ2, each object has a name and an age attribute, which is similar to saving in memory.

Step two: Call the encapsulated content from somewhere

When the encapsulated content is called, there are two scenarios:

    • Called directly through an object
    • Indirectly called through self

1. Direct invocation of encapsulated content by object

Shows how objects obj1 and Obj2 are saved in memory, so that the encapsulated content can be called according to the Save format: Object. Property name

Class Foo:     def __init__ (self, Name, age):        self.name = name        Self.age = Age obj1 = Foo (' Wupeiqi ', +) print obj1. Name    # Direct call to the Name property of the Obj1 object print obj1.age     # directly calls the Obj1 object's Age Property Obj2 = Foo (' Alex ',) print Obj2.name    # Call the Name property of the Obj2 object directly print Obj2.age     # Call the Age property of the Obj2 object directly

2. Indirectly invoking the encapsulated content via self

When executing a method in a class, the encapsulated content needs to be indirectly called through self

Class Foo:      def __init__ (self, Name, age):        self.name = name        Self.age = Age      def detail (self):        print Self.name        print self.age  obj1 = Foo (' Wupeiqi ', +) Obj1.detail ()  # Python will pass obj1 to the self parameter by default, which is: Obj1.detail ( OBJ1), so, at this point, the internal self = obj1, namely: Self.name is Wupeiqi; Self.age is obj2  = Foo (' Alex ',) Obj2.detail ()  # python defaults to Obj2 is passed to the self parameter, namely: Obj1.detail (OBJ2), so the self = Obj2 inside the method, that is: Self.name is Alex; Self.age is 78

  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 object directly or self to obtain the encapsulated content.

Ii. Inheritance

Inheritance, the inheritance in object-oriented is the same as the inheritance in real life, that is, the child can inherit the parent's content.

For example:

Cats can: Meow meow, eat, drink, pull, sprinkle

Dogs can: bark, eat, drink, pull, sprinkle

If we were to create a class for both cats and dogs, we would need to implement all of their functions for cats and dogs, as shown here:

Class Cat:    def meow meow (self):        print ' Meow meow '    def eat (self): # do        something    def drink (self): # do        something    def Pull (self):        # do something    def Caesar:        # do Somethingclass dog:    def barking (self):        print ' Meow meow ' C12/>def Eat (self):        # do something    def drink (self):        # do something    def pull (self):        # do something    def scatter (self):        # do something

  

The above code is not difficult to see, eat, drink, pull, and Satan is the function of both cats and dogs, and we are the cat and the dog's class has been written two times. If you use the idea of inheritance, implement as follows:

Animals: Eating, drinking, pulling, spreading

Cat: Meow Meow (cat inherits function of animal)

Dog: Barking (dogs inherit the function of animals)

Therefore, for object-oriented inheritance, it is actually the method of extracting multiple classes common to the parent class, and the subclass inherits only the parent class without having to implement each method in one.

So the question comes again, how to inherit?

    • Whether multiple classes can be inherited
    • If you have inherited multiple classes that have the same function in each class, then that one will be used?

1. Python classes can inherit multiple classes, and Java and C # can inherit only one class

2. If the Python class inherits more than one class, there are two ways to find the method: Depth first and breadth First

    • When a class is a classic class, multiple inheritance cases are searched in the depth-first way
    • When a class is a new class, in multiple inheritance cases, the breadth-first method is found

Classic class and new class, literally can see an old a new, new inevitably contain with many functions, is also recommended after the wording, from the wording of the words, if the current class or the parent class inherits the object class , then the class is a new class, otherwise it is the classic class.

Class D:    def bar (self):        print ' D.bar ' class C (d):    def Bar (self):        print ' C.bar ' class B (d):    def Bar ( Self):        print ' B.bar ' Class A (B, C):    def Bar (self):        print ' a.bar ' a = A () # when executing the Bar method # First go to Class A to find, if not in Class A, Then continue to find in class B, if there is a class B, then continue to find in Class D, if there is a class D, then continue to find in class C, if still not found, then error # So, look in order: A-----------and C # in the process of finding the bar method, once found, The search process is immediately interrupted and no further a.bar () will be found.

  

Class D (object):    def Bar (self):        print ' D.bar ' class C (d):    def Bar (self):        print ' C.bar ' class B (d):    def bar:        print ' B.bar ' Class A (B, C):    def Bar (self):        print ' a.bar ' a = A () # when executing the Bar method # First go to Class A to find, if not in Class A, Then continue to find in class B, if there is a class B, then continue to find in Class C, if there is a Class C, then continue to find in class D, if still not found, then error # So, look in order: A------and C--and d# in the process of finding the bar method above, once found, The search process is immediately interrupted and no further a.bar () will be found.

Classic class: Go first A class, if not in Class A, continue to B class, if there is one in class B, continue to D class, if there is in Class D, continue to C class, or if it is not found, the error

New class: First go to class a to find, if not in Class A, then continue to the class B to find, if there is a class B, then continue to the class C , if there is a Class C, then continue to find in class D , if still not found, The error

Note: In the above search process, once found, the search process immediately interrupted, and will not continue to find

Three, polymorphic

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

Class F1:    passclass S1 (F1):    def Show (self):        print ' S1.show ' class S2 (F1):    def Show (self):        print ' S2.show ' # Because of defining function parameters in Java or C #, you must specify the type of the parameter # in order for the Func function to execute the Show method of the S1 object, and to execute the Show method of the S2 object, the parent class of the S1 and S2 classes is defined # The actual parameters passed in are: S1 object and S2 object def Func (F1 obj):    "" "Func function needs to receive a F1 type or F1 subclass of type" "" "        print Obj.show ()    s1_obj = S1 () Func (s1_obj) # passes in the Func function the object of the S1 class S1_obj, executes the S1 's Show method, results: S1.shows2_obj = S2 () func (s2_obj) # passed in the Func function to the SS class object Ss_obj, executes the SS Show method, Result: S2.show

  

Class F1:    passclass S1 (F1):    def Show (self):        print ' S1.show ' class S2 (F1):    def Show (self):        print ' S2.show ' def Func (obj):    

  

On object-oriented

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.