Python Object-oriented OOP

Source: Internet
Author: User

I. Object-oriented and physiognomy processes

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.

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.

We use an example to illustrate the differences between process-oriented and object-oriented programming processes.

Let's say we're dealing with students ' grades, process-oriented programs that show a student score can be represented by a dict, which is represented by a function of the student's performance:

1>>> STD1 = {'name':'Mich','score': 100}2>>> STD2 = {'name':'Jack','score': 50}3>>>defPrint_score (STD):4...Print('%s:%s'% (std['name'], std['score']))5 ... 6>>>Print_score (STD1)7mich:1008>>>Print_score (STD2)9Jack:50

If we adopt the object-oriented program design idea, we prefer not to think about the program's execution flow, but this Student data type should be treated as an object, which owns the object name and both score properties. If you want to print a student's score, you first have to create the corresponding object for the student, and then send a message to the object to print_score let the object print its own data.

1>>>classStudent (object):2...def __init__(self, Name, score):3... Self.name =name4... Self.score =score5...defPrint_score (self):6...Print('%s:%s'%(Self.name, Self.score))7 ... 8>>> st1 = Student ('Mich', 100)9>>> St2 = Student ('Jack', 100)Ten>>>St1.print_score () Onemich:100 A>>>St2.print_score () -jack:100

The idea of object-oriented design is from the nature, because in nature, the concept of classes (class) and instances (Instance) is very natural. Class is an abstract concept, such as our definition of class--student, refers to the concept of students, and instances (Instance) is a specific Student, for example, St1 and St2 is two specific Student.

Therefore, the object-oriented design idea is to abstract the class, according to the class to create instance.

Object-oriented abstraction is also higher than a function, because a class contains both data and methods to manipulate the data.

Object-oriented three main features: encapsulation, inheritance, polymorphism

Python Object-oriented OOP

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.