A brief introduction to the concept of object-oriented programming in Python _python

Source: Internet
Author: User

Object-oriented programming--object oriented programming, called OOP, is a kind of programming idea. OOP takes objects as the basic unit of a program, and an object contains functions of data and manipulating data.

Process-oriented programming treats a computer program as a series of command sets, the sequential execution of a set of functions. In order to simplify program design, process-oriented function is divided into sub functions, that is, to reduce the complexity of the system by cutting the block function into small function.

While object-oriented programming treats computer programs as a set of objects, each object can receive messages from other objects and process them, and the execution of a computer program is a series of messages passed 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 classes (Class) in Object-oriented objects.

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

If we want to deal with a student's score sheet, a process-oriented program can be expressed as a dict to indicate a student's performance:

STD1 = {' name ': ' Michael ', ' score ': +}
std2 = {' name ': ' Bob ', ' Score ': 81}

The processing of student scores can be achieved through functions, such as printing students ' grades:

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

If we adopt object-oriented programming ideas, the first thing we think about is not the execution process of the program, but student this data type should be treated as an object that has both the name and score properties. If you want to print a student's score, you must first create the corresponding object for the student, and then send a Print_score message to the object and let the object print out 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 calling the object's corresponding associated function, which we call the object's method. Object-oriented programs are written like this:

Bart = Student (' Bart Simpson ', ^)
Lisa = Student (' Lisa Simpson ', Bart.print_score)
() Lisa.print_score
()

The idea of object-oriented design comes from nature, because the concept of class and instance (Instance) is natural in nature. Class is an abstract concept, such as our definition of class--student, refers to the concept of students, and the example (Instance) is a specific Student, such as Bart Simpson and Lisa Simpson is the two specific student:

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

Object-oriented abstraction is higher than function because a class contains both data and methods of manipulating data.
Summary

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

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.