Python road, Day6-object-oriented learning

Source: Internet
Author: User

Process-oriented VS object-oriented Programming Paradigm

Programming is a programmer with a specific syntax + data structure + algorithm composed of code to tell the computer how to perform the task of the process, a program is a programmer in order to get a task result and a set of instructions, is so-called all roads to Rome, the way to achieve a task there are many different ways, The types of programming that are summed up by the characteristics of these different programming methods are the programming paradigm. 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 programming (procedural programming)
Procedural programming uses a list of instructions to tell the computer what to do step-by-step.
Process-oriented programming dependencies-you guessed-procedures, a procedure contains a set of steps to be computed, and the process is called Top-down languages, which is the procedure from the top to the next step, step by step from top to bottom, to solve the problem from beginning to end. The basic design idea is that the program starts with solving a big problem, and then breaks down a big problem into many small problems or sub-processes, and the process continues to decompose until the small problem is simple enough to be solved within a small step.

Take a typical process-oriented example, database backup, three-step, connect database, backup database, test backup file availability.

Object-Oriented Programming

OOP programming is the use of "class" and "object" to create a variety of models to achieve a real-world description, the use of object-oriented programming because it can make the maintenance and extension 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.

Several core features of object-oriented are as follows

Class
A class is an abstraction, a blueprint, a prototype for a class of objects that have the same properties. The properties of these objects are defined in the class (variables (data)), common methods

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, there are different

Encapsulation Package
The assignment of data in a class, internal invocation is transparent to external users, which makes the class A capsule or container in which the data and methods of the class are contained.

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

Polymorphism Polymorphism
Polymorphism 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 parent class method 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.
Sending the same message to different classes of objects will have different behavior. For example, if your boss lets all employees start working at nine o'clock, he says "get started" at nine o'clock, instead of saying to the salesperson: "Start a sales job," say to the technician, "Start technical work", because "employee" is an abstract thing, so long as the employee can start to work, He knows that. As for each employee, of course, they do their job and do their jobs.
Polymorphism allows the object of a subclass to be used as an object of the parent class, a reference to a parent type to an object of its subtype, and the method called is the method of that subtype. The code that references and invokes the method is already determined before it is compiled, and the object pointed to by the reference can be dynamically bound during run time

Object-Oriented programming ( object-oriented ProgrammingFor beginners in programming languages, OOP is not an easy-to-understand programming approach, although everyone knows that the three main features of OOP are inheritance, encapsulation, polymorphism, and everyone knows how to define classes, methods, and other object-oriented common syntax, but when it comes to writing programs, Still a lot of people like to use functional programming to write code, especially beginners, it is easy to fall into a dilemma is "I know object-oriented, I will write classes, but I still do not find that after the use of object-oriented, to our program development efficiency or other aspects of what benefits, Because I can use functional programming to reduce duplication of code and do the program extensible, why also use object-oriented? ”。   For this reason, I personally think that because you do not fully understand the benefits of object-oriented, I will write a primer on the object-oriented article, I hope to help you understand and use object-oriented programming. No matter what the form of programming, we have to clearly remember the following principles:
    1. Writing duplicate code is a very bad low-level behavior.
    2. The code you write needs to change frequently.
The development of a formal program with the kind of writing a run-time throw the small script a very big difference is that your code always need to constantly change, not to modify the bug is to add new features, etc., so in order to facilitate program modification and extension, you write code must follow easy-to-read, easy to change the principle (professional data called good readability, Easy to expand). If you copy a piece of the same code, paste it into the program in a number of places to invoke the function in various parts of the program, then you can change the function in the future, you need to change the program in a number of places, the way to write the program is problematic, because if you accidentally missed a place not changed, That could lead to problems with the entire program running. So we know that in development we must try to avoid writing duplicate code, otherwise it is equivalent to digging holes for ourselves. Fortunately, the appearance of functions can help us to easily solve the problem of duplicate code, for the function that needs to be repeated calls, only need to write it into a function, and then call the function name directly in each place of the program, and when it is necessary to modify the function, just change the function code, and then the whole program is updated. In fact, the main role of OOP programming is to make your code modification and extension of the more easily, then the small white to ask, since the function can achieve this demand, but also the OOP dry yarn use it? Hehe, saying this is like, ancient times, people fight to kill all with knives, later came out of the gun, its main function with the same knife, but also kill, and then small White asked, since the knife can kill, that also gun dry yarn, haha, obviously, because the gun can be better faster and easier to kill. The main difference between functional programming and OOP is that OOP can make programs easier to scale and change. Object-oriented features: encapsulation

The package is best understood. Encapsulation is one of the object-oriented features and is the main feature of object and class concepts.

Encapsulation, which is the encapsulation of objective things into abstract classes, and classes can put their own data and methods only trusted class or object operation, to the untrusted information hiding.

Inherited

One of the main functions of object-oriented programming (OOP) language is "inheritance". Inheritance refers to the ability to use all the functionality of an existing class and to extend these capabilities without rewriting the original class.

New classes created through inheritance are called "subclasses" or "derived classes."

The inherited class is called the base class, the parent class, or the superclass.

The process of inheritance is from the general to the special process.

To implement inheritance, it can be implemented through inheritance (inheritance) and combination (composition).

In some OOP languages, a subclass can inherit multiple base classes. However, in general, a subclass can have only one base class, and to implement multiple inheritance, it can be implemented by multilevel inheritance.

There are 2 main implementations of the concept of inheritance: implementation inheritance, interface inheritance.

?         Implementation inheritance refers to the ability to use the properties and methods of a base class without additional coding; Interface inheritance is the ability to use only the names of properties and methods, but the subclass must provide the implementation (subclass refactoring method), and one thing to consider when using inheritance is that the relationship between the two classes should be a "belongs" relationship. For example, the Employee is a person and the Manager is a person, so these two classes can inherit the People class. But the Leg class cannot inherit the person class, because the leg is not a human. Abstract classes define only the generic properties and methods that will be created by the subclass.

OO development paradigm is roughly: dividing objects → abstract classes → organizing classes into hierarchical structures (inheritance and compositing) → Designing and implementing several stages with classes and instances.

polymorphic  Polymorphism (POLYMORPHISN) is a technique that allows you to set a parent object to be equal to one or more of his child objects, after which the parent object can operate differently depending on the attributes of the child object currently assigned to it. To put it simply, it is a sentence: A pointer to the parent class type is allowed to be assigned a pointer to the child class type. So what is the role of polymorphism? We know that encapsulation can hide implementation details and make code modular; Inheritance can extend existing code modules (classes); they are all designed to-code reuse. And polymorphism is for another purpose--interface reuse! The role of polymorphism is to ensure that classes are called correctly when inheriting and deriving a property of an instance of any class in the family tree.  Pyhon Many grammars support polymorphism, such as Len (), sorted (), you send Len a string to return the length of the string, and the list returns the length of the list.Domain Model

All right, you're going to get it now. Object-oriented syntax, then please look at the final job requirements of this chapter, I believe you may be blind, many students have learned the object-oriented grammar, but still can't write object-oriented program, why? The reason is because you have not mastered an object-oriented design tool, you say I read less don't cheat me, what weapon?

The answer is: domain modeling . Starting with the domain model, we begin the object-oriented analysis and design process, and we can say that the domain model is a bridge from requirement analysis to object-oriented design.

The domain model, as the name implies, is a modeling of the areas involved in the requirements, and the more popular is the business model. Refer to Baidu Encyclopedia (http://baike.baidu.cn/view/757895.htm ), the domain model is defined as follows:

From this definition we can see that the domain model has two main functions:

    1. Discover important Business Domain concepts
    2. Establishing the relationship between business domain concepts

domain modeling Zi jing

Domain models are so important that many students may think that domain modeling is complex and requires a high level of skill. However, in fact, the domain modeling is very simple, it is a little difficult to believe that the domain modeling method to summarize is " looking for a noun "! Many students see this method after the estimate will laugh out: too fake it, so simple, find a junior high school will Ah, that our company analysts and designers what is the use of OH?

Analysts and designers, of course, are useful, and we'll see later that even the simple search for a noun involves analysis and refinement, rather than simply extracting it, so that the experience and skills of analysts and designers can come in handy. But domain model analysis is also relatively simple, even without extensive experience and skill, at least to complete a working domain model.

Although we say "look for a noun" is very simple, but a key question has not been explained: where to find ? If you remember that the domain model is a "demand-to-object-oriented bridge", then you must be able to think of it all at once: from the requirements model, specifically from the use case.

The way to summarize domain modeling is to " look for nouns from use cases ". Of course, after finding the noun, in order to be able to more conform to the object-oriented requirements and characteristics, we also need to further refine these nouns, this is the next step: add attributes, even relationship !

Finally, we summarize the Zi Jing methods of domain modeling: finding nouns, adding attributes, and connecting relationships .

  

Find a noun

Who: Trainees, lecturers, administrators

Case:

1. The administrator created two campuses in Beijing and Shanghai

2. Administrator created Linux \ Python \ Go 3 courses

3. The administrator has created the Beijing campus Python phase 16, go development Phase I, and the Shanghai campus of the Linux 36 class

4. The administrator created a student in Beijing campus, and assigned it to the class Python phase 16

5. The administrator created the instructor Alex and assigned it to class Python phase 16 and full stack 5

6. Lecturer Alex has created a Python class record for phase 16 Day6

7. Lecturer Alex for Day6 This class all the students to give homework , Xiao Sunny got a, Li Lei got C, 严帅 got B

8. Students have submitted their homework in the day6 of Python 16

9. Student Li Lei looked at all the courses he had reported

10 student Li Lei was looking at his list of achievements in the PY16 period and then killed himself.

11. Student Xiao Ching and the lecturer Alex confessed

List of nouns:

Administrators, campuses, courses, classes, class records, assignments, grades, lecturers, students

Plus properties

Connected relationship

With classes and attributes, the next step is to find out their relationships.


Work in this section: Course selection system

Role: School, student, course, lecturer
Requirements:
1. Creation of 2 schools in Beijing and Shanghai
2. Create Linux, Python, go 3 courses, linux\py Open in Beijing, go in Shanghai
3. Courses include, cycle, price, create a course through the school
4. Create classes through schools, class-related courses, lecturers
5. When creating a student, select the school, associate the class
5. To associate the school when creating the instructor role,
6. Two role interfaces available
6.1 Student view, can register, pay tuition, choose class,
6.2 Instructor View, instructors can manage their own classes, select classes in class, view class student list, modify the results of the students managed
6.3 Manage views, create instructors, create classes, create courses

7. Data generated by the above operations are saved to the file via pickle serialization

Python road, Day6-object-oriented learning

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.