Python object-oriented (classes and objects)

An object-oriented programming

Object-oriented --Object Oriented Programming, referred oop, is a programming idea. For example: If to buy a car, you need to go 4S shop to buy a car, respectively, to go to the insurance company, go to the Inland Revenue Department, and finally to the traffic control on the card, respectively for each of them.

Process-oriented - process-oriented thinking is to a project, one thing in a certain order, from start to finish to do so step by step, what to do first, what to do after, until the end. For example, just to buy a car thing, in reality, we are required to go to the 4S shops, insurance companies, Inland Revenue Department, the four local traffic control, and process-oriented, that is, to gather them into one, we put this place is called the buying a car, the car we do not need to go to many places, only to find the need to buy a car at a responsible person can do all things in the yard.

Second, the object-oriented features

Class: class

Class, compared to the real world, it is a kind, like a model, a blueprint.

A class object that is a class have the same attributes abstract, blueprints, prototypes.

Defines the properties (variables (data)), the common method of these objects are included in the class.

Object: object

Object, also refers to the model made out of concrete things.

Instantiate an object that is an instance of the class, a class must be called in the program only after instantiated, a class can instantiate multiple objects, each also can have different attributes, just like humans refers to all people, everyone refers to a specific target, before people have in common, there are also different.

Instantiation:

Initialize a class, create an object. To a class of objects into a specific process, called instantiation.

Package:

The implementation details of some of the features are not exposed to outside, the class assignment of data internal to external calls transparent to the user, which makes the class into a container or capsule, which contains the data and methods of a class.

For example, a man-made, you take what his heart lung and kidney liver and spleen are encapsulated inside the body, other people can not see, you go directly to that person.

inherit:

A class can derive subclasses, attributes defined in the parent class, method automatically inherited by subclasses. Let's say you inherited your father's name.

python3 in multiple inheritance are breadth-first, python2 multiple inheritance in the classic category is depth-first, multiple inheritance is a new class of preference breadth.

Inheritance is for code reuse

Polymorphic:

They sent the same message to different classes of objects will behave differently. For example, your boss so that all employees start work at nine o'clock, he was just nine o'clock, said: "start work" can be, without the need for sales staff said: "start sales," said technician : "start technical work" because "employee" is an abstract thing, as long as the employee can start work, he knows it on the line. For each employee, of course, they will carry out their duties, do their work.

Polymorphism is a reflection of abstraction, the common number of specific things abstracted, then, dialogue with different specific things through this abstract thing.

Third, the class

Some concepts:

Property: is a property of the class which is variable with the class and instance variables and class variables when class variables are as defined there, and instance variable is only produced in the instantiation. This can be understood as a kind man, his name, age and sex is its property.

Methods: The class is a function that is defined in the function inside the class that implements a function, for example, people have to sleep function.

Constructors: What is the constructor, the class is instantiated in a certain initialization time to do, like people, you build a car when it must have color, model and so on.

Destructor: destructor are some instances when this operation do destroyed.

Defined categories:

Define a class using the class keyword, category General, when we developed the first letter capitalized. There are classic python classes and new classes, there is no difference in python3 They are, in python2 classic category is depth-first in a multiple inheritance when a new class of breadth-first. python3 unified breadth priority, he said this back in the succession of time will say.

 

 

Guess you like

Origin www.cnblogs.com/brf-test/p/11845642.html