2015-12-04 Study notes Finishing

Source: Internet
Author: User

Object-oriented programming--object oriented programming, short for OOP, is a programming idea. OOP takes objects as the basic unit of a program, and an object contains functions for data and manipulating data. (can be called attributes and methods, respectively)

Object oriented

Process-oriented programming treats a computer program as a set of commands, which is the sequential execution of a set of functions. In order to simplify the program design, the process will continue to cut the function into sub-function, that is, the large function by cutting into small block function to reduce the complexity of the system.

Object-oriented programming treats a computer program as a set of objects, and each object can receive messages from other objects and process them, and the execution of a computer program is a series of messages passing between objects.

In Python, all data types can be treated as objects and, of course, objects can be customized. The custom object data type is the concept of class in object-oriented.

If we want to deal with some people's information data, such as age, then the process-oriented approach is:

#!/usr/bin/env python#-- Coding:utf-8- -__author__='Echorep'#use a dictionary to define the age of two peopleMan1 = {"name":"Alex"," Age": 18}man2= {"name":"Echo"," Age": 16}#define function output name, agedefPrintage (man):Print "%s is%s"% (man["name"],man[" Age"])#calling Functionsprintage (man1) printage (man2)

In the object-oriented, there is a change of thinking:

#!/usr/bin/env python#-- Coding:utf-8- -__author__='Echorep'"""using object-oriented program design idea, we first think about not the program execution flow, but man this data type should be treated as an object, this object has name,age these two properties. If you want to print a person's results, you must first create a corresponding object for this person, and then, send an Printname message to the object, so that the object themselves to print themselves out. """#The template that creates the object is the classclassMan (object):def __init__(self,name,age): Self.name=name Self.age= AgedefPrintname (self):Print "%s is%s"%(self.name,self.age)#Instantiation of a class is called an instanceAlex = Man ("alse", 18) echo= Man ("Echo", 16)#function that invokes an objectalex.printname () echo.printname ( )

Therefore, the object-oriented design idea is to first abstract out the class and then instantiate the instance according to the class.

An instance has no methods, but has attributes.

There are methods for classes with attributes, but attributes are not real.

A class contains both data and methods that manipulate data, so object-oriented abstraction is higher than a function.

Data encapsulation, inheritance and polymorphism are the three main features of object-oriented. Classes and instances

Object-oriented concepts are classes (class) and instances (Instance), and it is important to keep in mind that classes are abstract templates, such as the Man class, and that instances are specific "objects" that are created from classes (the process of converting classes to instances is called instantiation) and each object has the same method. But the individual data may be different.

In the case of Pythong, the definition class is implemented by the Class keyword:

class Man (object):     Pass

The class name, the man, followed by the class name is usually the first word in uppercase, followed by (object), which is the class from which the class inherits, usually without an appropriate inheriting class, using the object class, which is the class that all classes will eventually inherit.

Once you have defined the man class, you can create an instance of mans based on the Mans class, and create an instance by using the class name + ():

You are free to bind a property to an instance variable, such as binding a Name property to Echo:

" Echo Rep " echo.printname ()      is 16

When creating an instance, force some of the attributes that we think must be bound to be filled in. By defining a special __init__ method, when creating an instance, bind the attributes such as Name,age:

class Man (object):     def __init__ (self,name,age):         = name        = Age

It is important to note that the first argument of the __init__ method is always self, which represents the created instance itself, so within the __init__ method, you can bind various properties to self, because it points to the created instance itself.

With the __init__ method, when you create an instance, you cannot pass in an empty argument, you must pass in a parameter that matches the __init__ method, but self does not need to be passed, and the Python interpreter will pass the instance variable in itself:

" Echo Rep "  =17    

A function defined in a class is only a bit different than a normal function, which is that the first argument is always the instance variable self and, when called, does not pass the argument. In addition, class methods and ordinary functions are not different, so you can still use the default parameters, variable parameters, keyword parameters and named keyword parameters.

Data encapsulation

An important feature of object-oriented is data encapsulation. In the previous man class, each instance has its own name and age data. We can access this data through functions, such as printing a person's age:

def Printname (self):         Print " %s is%s " %(self.name,self.age) .... Echo.printname ()    is 17

Since the man class itself has this data, to access the data, there is no need to access the function from outside, you can directly within the man class to define the function of accessing the data, so that the "data" to encapsulate. The functions of these encapsulated data are associated with the man class itself, which we call the method of the class:

# The template that creates the object is the class class Man (object):     def __init__ (self,name,age):         = name                     = Age def printname (self): print"  %s is%s" % (self.name,self.age)

To define a method, the other one is the same as the normal function except that the first argument is self. To invoke a method, just call it directly on the instance variable, except that self does not pass, and other parameters are passed in normally:

So, looking outside the man class, just know that creating an instance requires name and age, and how the print is defined inside the man class, the data and logic are "encapsulated", the invocation is easy, but the internal details are not known.

Another benefit of encapsulation is the ability to add new methods to the man class.

In short, the class is the template to create the instance, and the instance is a concrete object, each instance has the data are independent of each other, the method is bound with the instance of the function, and the normal function, the method can directly access the data of the instance, by invoking the method on the instance, we directly manipulate the data inside the object, But there is no need to know the implementation details inside the method.

Reference Link: http://www.liaoxuefeng.com/wiki/0014316089557264a6b348958f449949df42a6d3a2e542c000/ 0014318645694388f1f10473d7f416e9291616be8367ab5000

2015-12-04 Study notes Finishing

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.