Nine: Object-Oriented

1.OOP

Object oriented python can not solve all the problems with the function of programming, OOP has let become more organized.

But java is fully object-oriented, all of which are encapsulated in the class, method alone can not appear outside the class .python natural polymorphic, java inside Polymorphism with interfaces and superclasses

Functional programming face: in most cases the data is passed inside a function within the function, the result of the function to be implemented to meet requirement

Object-Oriented Programming: The initialization of the data package to the inside of the object, the method using data directly inside the object.

And function data for logically separated, object-oriented data and functions which are encapsulated in a permanent memory objects, to facilitate subsequent re-use.

From the IDE perspective, so much the function name function-oriented programming can not remember, then the object-oriented method to solve a class of problems into a class, create objects initialized incoming data, holding the object can be resolved such problem, the object method name, because there are no tips to remember.

1.1 Object-oriented function for VS

Define a simple function-oriented, calling simple, function-oriented inside * args and ** kwargs is actually a package of meaning, but the transfer of data is random number, accessible only by index

oop by initialization method, the encapsulated data to an object which, when used directly self. attribute name may take the value of the attribute name no. not come out, but also not obtain the value of the data range constraints.

Object-oriented definition of complex, call the complex, but the method which will solve a class of problems have been classified logically clearer structure, more in line with human thinking.

If no function is programmed default parameters, each call stack frame to be open, pass data.

Object-oriented, to create an object with the data into a class, a method which calls data directly, without the call when the transmission parameters, the method is configured by OOP encapsulated data into objects.

Performance: for example, a function dedicated to open a file, all the way to the first step is to open a file, call completion over, consuming disk IO performance, then open a channel to do with the best, do not always use until, rather than use it to open, do not shut up on

The OOP at initialization time to put a f = open () opens a file, then the object is always to hold the file handle, if the transmission network is to open the file, it is more a performance fee, the program that has been blocked in waiting connect to the server, and then open the file.

1.2 classification

Programming function can not be classified, i.e. the module with the same category py file in a file py same functions not previously learned OOp

java in a class file is a file which put the issue of dealing with such data and functions are placed in a class

python, more flexible, py file can have functions, there may be classes, may be the same function as java package into the class, although there is no relationship between classes, and the classes are also data which has nothing to do, it is packaged as @staticmethod

1.3OOP step

  • + Extract classified public value, which is the thrust reversers
  • Before writing already had an idea, write all the code associated with the class directly in the class.

2. Three characteristics

2.1 package

Encapsulated into data objects.

Method Packaging category, data object sharing also be encapsulated into the class.

Private data only way to restrain alias

2.2 inheritance (in order to "lazy" Reuse)

  • The default object class inheritance,
  • Find __mro__ follow the rules to inherit,
  • Personalized support multiple inheritance, can have multiple father, or to comply with __mro __ (), and more complex multiple inheritance
  • Private data can not be inherited
  • Now find themselves in the class, not to find the parent class, until the object, it is also not on the error
  • Diamond diamond inheritance

Guess you like

Origin www.cnblogs.com/gyxpy/p/11618421.html