Object oriented
Introduction (Attributes (class, Object---> Encapsulation, inheritance, polymorphism) 、---> All things in the world can be categorized; all things in the world can be objects
As long as the object, it must belong to a category, as long as the object, there must be attributes
You're God. Earth---> Mountains, rivers, seas, forests
---> Birds-fly, eat worms, lay eggs, (hundreds of kinds of birds (cuckoo-fly, eat worms, lay eggs, sing, crow-fly, eat worms, lay eggs ...) ), Beast (Lion-King of beasts; Tiger-King of the Forest)
smelly fish, rotten shrimp,----> People -think, speak, eat and sleep
Syntax (attributes, methods, constructors, destructors),
(Private method, private property), (class variable, instance variable)
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 program from the top to the next step, step by step from top to bottom, 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. the code is as follows
1 defdb_conn ():2 Print("Connecting DB ...")3 4 5 defDb_backup (dbname):6 Print("Export Database ...", dbname)7 Print("Package The backup file and move it to the appropriate directory ...")8 9 defdb_backup_test ():Ten Print("Import the backup file into the test library to see if the import was successful") One A - defMain (): - Db_conn () theDb_backup ('my_db') - db_backup_test () - - + if __name__=='__main__': -Main ()View Code
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 .
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
Python Learning Note _week6