Python Road-python (object-oriented one)

Source: Internet
Author: User

First, object-oriented introduction

Second, why to use object-oriented development

Three, encapsulation, inheritance, polymorphism, class, method

Process-oriented and object-oriented differences

Programming Paradigm:

    In general, to get a project different two people have different ways of writing (the same is the fixed syntax, data structure). The programming paradigm, which summarizes the characteristics of these different programming methods, is the programming pattern. Different programming paradigms essentially represent different solutions to various types of tasks, and most languages support only one programming paradigm, and of course some languages can support multiple programming paradigms at the same time. Two of the most important programming paradigms are process-oriented programming and object-oriented programming.

Process oriented (procedural programming):

    The so-called process is to complete a demand, will be a large demand, divided into different modules, or even smaller modules (methods) inside, and then the program from the next step to complete the calculation of execution. The students in general learn the basics, but there is a problem. Everyone writes a module (method) that invokes the result of the next module, and possibly multiple methods take the result of a function execution. Imagine that if I need to add or decrease a function, all functions that call this function (method) result need to be modified. Such a silly writing process is particularly impractical, if the script can be used. If you do a project, there are often different needs, which is tantamount to digging a hole for themselves. This can be understood as a process-oriented programming.

  Object-oriented (object-oriented programming):

    OOP programming is "class" and "object" to create a variety of models to achieve the real-world description, using object-oriented programming because it can make the maintenance and expansion of the program easier, and can greatly improve the efficiency of program development, in addition, An object-oriented program can make it easier for people to understand your code logic and make team development easier.

All things in the world can be classified. All things in the world are objects. As long as the object, it must belong to a certain category. As long as the object, there must be attributes.

  Object-oriented features:

Class
A class is an abstraction, a blueprint, a prototype for a class of objects that have the same properties. The properties and common methods of these objects are defined in the class. is to abstract a number of things that have the same attributes, and then define a category.

Object objects
An object is an instantiated instance of a class, a class must be instantiated before it can be called in the program, a class can instantiate multiple objects, each object can also have different properties, like human refers to everyone, each person refers to the specific object, people and people before there is a common, but also different. In fact, after defining a class, some of the ways in which you define this thing in your class are (in fact, attributes). Define the class, you have to do it, for example, this class is to build machines, the machine is created object, that is, an example.

Encapsulation Package
The assignment of the data in the class, the internal invocation is transparent to the external user, which makes the class A capsule or container, with the data and methods of the class contained in the bread. Encapsulation is the internal do not want to see the external data to block, only the internal can see the call, external cannot find.

Inheritance inheritance
A class can derive subclasses, properties, methods defined in the parent class, automatic quilt class inheritance

Polymorphism Polymorphism
State is an important feature of object-oriented, simple point: "An interface, a variety of implementations," refers to a base class derived from a different subclass, and each subclass inherits the same method name, but also the method of the parent class to do a different implementation, this is the same thing shows a variety of forms.
Programming is actually a process of abstracting the concrete world, which is a manifestation of abstraction, abstracting the common denominator of a series of specific things, and then through this abstract thing, and dialogue with different concrete things.

To give an example: Send a command, for example, I am the boss to build a building. Sales are going to sell buildings, contractors want to buy materials, contractors are looking for workers. I only say a word, his family began to work separately, instead of I want one to urge the explanation.

1 classDos: #定义一个类狗2 def Bulk (self): #类里面有对象狗, you're going to scream.3Print"let me have a cry") #具体怎么叫4 5D1 =Dos () #生成一条狗6D2 =Dos () #再生成一条狗7D3 =Dos () #再来一条狗8 9 d1.bulk () #狗1叫Ten d2.bulk () #狗2叫 One d3.bulk () #狗3叫 A>>>let me have a cry ->>>let me have a cry ->>> Call a sound
A simple class is defined.
1 classDos:2     def __init__(self,name):#This class instantiation, in fact, is to pass the value of the class3Self.name =name4 5     defBulk (self):6         Print("%s is called a sound"%self.name)7 8D1 = Dos ("Dog One")9D2 = Dos ("Dog Two")TenD3 = Dos ("Dog Three") One  A D1.bulk () - D2.bulk () - D3.bulk () the>>>A dog called a cry ->>>two dogs and a shout ->>> Dog three Call a sound



  

    

    

Python Road-python (object-oriented one)

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.