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.
1. Process-oriented programming (procedural programming)
is the program 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.
The problem is also obvious, that is, if you want to modify the program, you have to modify the part of the dependent parts you also have to modify, for example, if the program at the beginning you set a variable value of 1, but if other sub-procedures rely on the value of 1 of the variable to run correctly, If you change this variable, then you have to modify this process, if there is another subroutine dependent on the sub-process, it will have a series of effects, as the program becomes larger, the maintenance of this method will be more difficult.
So we generally think that if you just write some simple scripts to do some one-off tasks, it's great to use a process-oriented approach, but if the task you're dealing with is complex and needs to be iterative and maintainable, it's the most convenient way to use object-oriented.
2. Object-oriented programming (object oriented programming)
Object-oriented programming is a way of programming, which requires "classes" and "objects" to be implemented, so object-oriented programming is actually the use of "classes" and "objects".
The reason for using object-oriented programming is that it makes it easier to maintain and extend programs, and can greatly improve the efficiency of program development, and the object-oriented program makes it easier for people to understand your code logic, which makes team development easier.
2.1 Classes and objects
A class is a template that can contain multiple functions in a function, and---function is called a method in a class.
An object is an instance created from a template that can execute functions in a class through an instance object;
#定义一个类, class is the syntax for defining classes, the user is the class name, and (object) is the style of the new class, which must be written; What is a new class? Forget him for the time being! ) class User (object): def __init__ (self,name): #初始化函数, (Self,name) is the property to initialize, where self is a special parameter, required; name is the actual argument Self.name = name#--assigns the value of name to Self.name def uname (self): # Creates a method in the---class uname print ("%s is the best!"% self.name) i = User (' dd ') #---Create Object Ii.uname () based on class user #执行uname方法
2.2 Object-oriented three major features
2.2.1 Package
Encapsulation, as the name implies, encapsulates the content somewhere and then calls the content that is encapsulated somewhere.
Therefore, when using object-oriented encapsulation features, you need:
- Encapsulate content somewhere
- To invoke the encapsulated content from somewhere
First step: encapsulate the content somewhere
Self is a formal parameter that when executed Obj1=foo (' cc ', 18), self equals obj1;
When executing obj2=foo (' Coco ', 22), self equals obj2;
So, the content is actually encapsulated in objects Obj1 and OBJ2, each object has a name and an age attribute, which is similar to saving in memory.
Step two: Call the encapsulated content from somewhere
When the encapsulated content is called, there are two scenarios:
- Called directly through an object
- Indirectly called through self
1. Direct invocation of encapsulated content by object
Shows how objects obj1 and Obj2 are saved in memory, so that the encapsulated content can be called according to the Save format: Object. Property name
Class Foo: def __init__ (self, Name, age): self.name = name self.age = ageobj1 = Foo (' cc ') print (obj1.name) # Call The Name property of the Obj1 object directly print (obj1.age) # Call the Obj1 object's age property directly obj2 = Foo (' Coco ') print (obj2.name) # Directly calling the Name property of the Obj2 object Print (obj2.age) # Call the Age property of the Obj2 object directly----------------------------print out-------------------------------------cc18coco22
2. Indirectly invoking the encapsulated content via self
When executing a method in a class, the encapsulated content needs to be indirectly called through self
# Create a class, the class name is Class_basisclass class_basis: # Creates a method in the class RET def ret (self,): # Outputs the memory address of the self (" METHOD Ret's Self memory Address ", ID (self)) # creates an object obj, appended with the class name, obj = class_basis () # Output object obj memory address print (" Obj object memory address ", ID (obj)) # The RET method in the class is called through the object Obj.ret ()-------------------printout---------------------------obj Object memory address 2513776 method RET's Self memory address 2513776
The above test can clearly see the obj object and the method of the class self memory address is the same, then the method is self equal toobj;
----------self是形式参数,由python自行传递
In Summary, for object-oriented encapsulation, it is actually using the construction method to encapsulate the content into the object, and then indirectly through the object directly or self to obtain the encapsulated content. in Summary, for object-oriented encapsulation, it is actually using the construction method to encapsulate the content into the object, and then indirectly through the object directly or self to obtain the encapsulated content.
2.2.2 Inheritance
2.2.3 Polymorphism
Python Learning Notes-(10) Object-oriented basics