Object-oriented basic knowledge points summary

1. Programming paradigm:

Programming is the process by which programmers tell the computer how to perform tasks by using codes composed of specific syntax + data structures + algorithms.

A program is a set of instructions written by a programmer to get the result of a task. As the saying goes, all roads lead to Rome. There are many different ways to implement a task. The characteristics of these different programming methods are summarized. The resulting programming style category is the programming paradigm. Different programming paradigms essentially represent different problem-solving ideas for various types of tasks. Most languages ​​only support one programming paradigm, and of course some languages ​​can support multiple programming paradigms at the same time. The two most important programming paradigms are procedural programming and object-oriented programming.

Second, procedural programming

Process-oriented: The core is the word process, and the process refers to the steps to solve problems, design an assembly line, and a mechanical way of thinking.

The basic design idea is that the program starts by solving a big problem, and then decomposes a big problem into many small problems or sub-processes, and the process of executing these sub-processes continues to decompose until the small problems are simple enough to be solved in one. Solved in small steps.

Advantages: Complex problems are streamlined and simplified

Disadvantages: poor scalability. In the following example, if you want to extend the function of interactive(), such as adding authentication information, then the following functions are forced to make corresponding changes.

3. Object Oriented Programming ( Object Oriented Programing )

OOP (Object Oriented Programing ) programming is the use of "classes" and "objects" to create various models to achieve the description of the real world.

1. ( 1) Object-oriented: The core is the word object, and the object is the combination of features and skills

(2) Advantages: Strong scalability, just to solve the problem of scalability, does not mean to solve all problems. A must-have skill!

(3) Disadvantage: high programming complexity

In practice, specific problems need to be analyzed in detail. For those with low scalability requirements, process-oriented programming can be used, but most programs have high scalability requirements, so object-oriented programming is a must-have skill!

(4) Application scenarios: user needs change frequently, Internet applications, games, internal enterprise applications

(5) A class is a combination of similar characteristics and skills of a series of objects

(6) Emphasis: From different angles, the classification obtained is different

(7) In the real world: there must be objects first, then classes

(8) In the program: the class must be defined first, and then the class must be called to generate the object

2. (1 ) Explanation of terms

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

Attributes: Humans contain many features. If these features are described by programs, they are called attributes. For example, age, height, gender, name, etc. are called attributes. In a class, there can be multiple attributes.

Method: Humans not only have attributes such as height, age, and gender, but also can do many things, such as speaking, walking, eating, etc. Compared with attributes, which are nouns, speaking and walking are verbs. These verbs are described by programs and are called methods.

Instance (object): An object is an instantiated instance of a class. A class must be instantiated before it can be called in a program. A class can instantiate multiple objects, and each object can also have different attributes. Like human beings refers to all people, each person refers to a specific object, people have commonalities and differences between people

Instantiation: The process of turning a class into an object is called instantiation

(2 ) Three major characteristics of object-oriented

<1>Encapsulation package

The assignment of data and internal calls in the class are transparent to external users, which makes the class a capsule or container, which contains the data and methods of the class

<2>Inheritance inheritance

A class can derive subclasses, and the properties and methods defined in this superclass are automatically inherited by subclasses

<3>Polymorphism Polymorphism

Polymorphism is an important feature of object-oriented. To put it simply: "one interface, multiple implementations", it means that different subclasses are derived from a base class, and each subclass inherits the same method name at the same time. The methods of the parent class are implemented differently, which is the multiple forms of the same thing.

Programming is actually a process of abstracting the concrete world. Polymorphism is an embodiment of abstraction. It abstracts the common points of a series of concrete things, and then communicates with different concrete things through this abstract thing.

Sending the same message to objects of different classes will behave differently. For example, if your boss asks all employees to start work at nine o'clock, he only needs to say "start work" at nine o'clock, instead of saying "start sales work" to the salesperson, say to the technical staff : "Start technical work", because "employee" is an abstract thing, as long as it is an employee, he can start work, he knows it. As for each employee, of course, they will perform their own duties and do their own work.

Polymorphism allows an object of a subclass to be used as an object of the superclass, a reference of a supertype points to an object of its subtype, and the method invoked is the method of the subtype. The code that references and calls the method here is determined before compilation, and the object pointed to by the reference can be dynamically bound at runtime

3. How to operate the class

( 1 ) The code in the class will run when the class is defined

( 2 ) The first major operation is the operation of attributes, and the second major operation is to instantiate and generate objects one by one

4. How to operate objects

(1) __init__ method:

After adding the __init__ method, the instantiation steps:

<1> First generate an empty object (student1)

<2> Like this: whw_school.__init__(student1,'wanghw','male',27), pass the previously created empty object student1 and the following three values ​​to the __init__ method at the same time.

 

(2) Use of objects, zodiac search

<1> An object is a combination of characteristics and skills , and a class is a combination of a series of similar characteristics and similar skills .

<2> To generate an object in Python, the object itself has only its own unique characteristics, but if it has similar skills and characteristics as other objects, it needs to be placed in the "class".

<3> The data attributes in the class are common to all objects (id(student1.school) is the same as id(student2.school)).

<4> The function properties of the class are bound to objects, and binding to different objects is different binding methods

<5> When the object calls the binding method, the object itself will be passed in as the first parameter. If there is a second parameter, how to pass it.

<6> When an object is looking for an attribute, it should first look for it in its own space, and if not, then look for it in the class. If there is no such thing in the class, then look for it in the parent class, it will not look globally~~

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325116059&siteId=291194637