Python Object-oriented programming

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.

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.

Suppose we want to deal with the student's score table, in order to indicate a student's achievement, the process-oriented program can be represented by a dict:

std1 = { ‘name‘: ‘Michael‘, ‘score‘: 98 }std2 = { ‘name‘: ‘Bob‘, ‘score‘: 81 }

The process of student performance can be achieved through functions such as printing students ' grades:

def print_score(std):    print ‘%s: %s‘ % (std[‘name‘], std[‘score‘])

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.

class Student(object):    def __init__(self, name, score):        self.name = name        self.score = score    def print_score(self):        print ‘%s: %s‘ % (self.name, self.score)

Sending a message to an object is actually a call to the object's corresponding associated function, which we call the object method. The object-oriented program is written like this:

bart = Student(‘Bart Simpson‘, 59)lisa = Student(‘Lisa Simpson‘, 87)bart.print_score()lisa.print_score()

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, which refers to the concept of a student, while an instance (Instance) is a specific Student, for example, Bart Simpson and Lisa Simpson is a 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.

Summary

Data encapsulation, inheritance, and polymorphism are the three main features of object-oriented, which we will explain in detail later.

Python Object-oriented programming

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.