Object-Oriented Programming

Source: Internet
Author: User

An object-oriented programming and process-oriented programming 1 thinking of process-oriented programming

The core of the idea is the process, which refers to the steps to solve the problem, that is, what to do first and what to do, just like the assembly line, must step-by-step, based on process-oriented programming is a mechanized way of thinking.

Advantages : Complex problem of process, simplification;

cons : Poor scalability.

Application Scenario : Once the finish is almost unchanged, like the Linux kernel, git, and Apache HTTP server

2 Object-oriented programming ideas

With the rapid development of hardware, more and more complex business requirements, as well as more and more extensive programming applications, process-oriented programming has been unable to meet the demand, and then another programming idea began to rise, that is, object-oriented programming.

The core of the idea is the object, the object is a combination of characteristics and skills, based on the object-oriented design program is like the creator, want to create what to create, and the process-oriented mode of thinking in sharp contrast, object-oriented more attention to the real world of abstraction.

Advantages : It solves the extensibility of the program. A single modification of an object will immediately reflect the entire system.

cons : 1. Programming complexity is higher than process-oriented programming, not understanding object-oriented and immediately get started object-based design program, prone to over-design problems. The use of object-oriented objects in scenarios with low extensibility requirements can increase programming difficulty.

    1. Unable to process-oriented programming flow-style can be very accurate prediction of the problem of the processing process and results, object-oriented program once the beginning of the interaction between the object to solve the problem.

Application Scenario : frequently changing requirements of software, general requirements of the changes are concentrated in the user layer, Internet applications, enterprise internal software, games, etc.

Object-oriented programming is not all. For a software quality, object-oriented programming is only used to address extensibility.

A good application software should include the features:

Definition of Class II and object class

Class is the most important concept of object-oriented design, which is the combination of feature and skill, while class is a series of abstract concepts with similar features and skills.

In reality, objects appear first, and then different classes are defined based on a series of objects with the same characteristics, and the object is concrete, and the class is just a concept, not a real existence.

In the program is to define the class, and then according to the class to produce the object, which is similar to the use of the function, first define the function, the same class, in the program needs to define the class, after the call class, unlike the function, the calling function executes the function body code returns the result of the function, and the calling class produces and returns an

In the program, be sure to define the class first, and then use the object.

    1. Features use variable identifiers in programs, and skills are identified with functions.
    2. Thus, the most common of the classes is the definition of variables and functions.
# 定义一个类class something:    school = ‘hashangda‘    def learn(self):        print(‘something is learning‘)            def eat(self):        print(‘something is eating‘)            def sleep(self):        print(‘something is sleeping‘)

Attention:

    1. Classes can be arbitrary Python code, which is executed at the class definition stage;
    2. Thus, a new namespace is generated, which holds the variable name and function name of the class, which can be viewed by something._Dict_;
    3. The dictionary can be used by the classic class to manipulate the name of the class namespace (the new class has limitations), but Python provides us with a special syntax for access;
    4. The syntax of the Access property, the name defined in the class, are the properties of the class (variables and functions).
The use of classes in two programs

In a program. To access the properties of a class, the operation is _dict_

something.school # 等于经典类 something.__dict__[‘school‘]something.school = ‘oldboy‘ # 等于经典类 something.__dict__[‘school‘] = ‘oldboy‘something.name = ‘musibii‘ # 等于经典类 something.__dict__[‘name‘] = ‘musibii‘del something.name # 等于经典类 something.__dict__.pop(‘name‘)

Invoking a class (instantiating an object) in a program, producing and returning an object that produces an object that has properties inside the class and can be accessed through a period method.

Summarize:

    1. The nature of a class is a namespace, or a container for storing variables and functions;
    2. One of the purposes of a class is to use the name space to remove the name from its interior;
    3. The primary purpose of the class is to invoke the class to produce the object.
Use of three objects

By invoking a class-generated object as an instantiation of the class, the return value of the calling class is called an object/instance of the class.

some = something() # 产生一个实例对象,对象可以通过句点法访问类中的属性()print(some.school)# 结果 hashangda

A property defined in a class is a property that is common to all objects produced by the class, so how do you define each object's own unique properties?

class something:    school = ‘hashangda‘        def __init__(self, name, age, gender):        self.name = name        self.age = age        self.gender = gender

The init method in the class is used to initialize an instance object, and the following code is executed, so the object being instantiated is required to pass in the subsequent arguments.

some1 = something(‘musibii‘, 18, ‘male‘)some2 = something(‘thales‘, 20, ‘female‘)some3 = something(‘maffia‘, 22, ‘male‘)

In addition to the school property values, the other objects are unique property values.

The calling class produces a

    1. First, an empty object some1 is generated and then returned;
    2. Triggers the execution of the function init in the class, passing the object into init with the specified parameters in the enclosing class.

Summary: Init functionality that initializes its own unique feature (cannot have a return value) for an object at instantiation time

Four-Attribute Lookup

The property lookup, like the previously learned namespace lookup order logic, first looks in the object's namespace, does not go to the class namespace lookup, but does not go to the global namespace to find the object's properties.

Variables defined in a class are shared by all objects, objects can be used, classes can be used, and once a class changes the value of its own property, all objects are changed as well.

Five binding methods

A variable defined in a class is a data property of a class, both classes and objects can be used, and the value of the property points to a memory address;

A function defined in a class is a function property of a class, a class and an object can be called, a class is called a normal function, but a function defined in a class is used for the object and is bound to the object.

    1. function of the class: pass several parameters
    2. A binding method that points to a function of a class: What is special is the object to which the object is to be called, and the object itself is automatically passed in as the first argument when called.
All six objects

The concept of classes and types is unified in Python3, meaning that a class equals a type, such as a data type in Python, and we can define our own classes.

class muSibii:    passmusibii = muSibii()print(type(musibii))# 结果 <class ‘__main__.muSibii‘>

Object-Oriented Programming

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.