The ordinary way of Python (6)

Source: Internet
Author: User

introduction of Object-oriented programming1 Programming Paradigm:the programming paradigm (programming Paradigm) is a typical programming style of a programming language or a programming approach. With the development of programming methodology and software engineering, especially the popularization of OO ideas, the terms of paradigm (PARADIGM) and programming paradigm are appearing in front of people. Object-oriented Programming (OOP) is often hailed as a revolutionary idea, because it differs from other programming paradigms. The programming paradigm is perhaps the most important term to understand when learning any programming language. The programming paradigm is a sort of programming language that is not intended for a programming language. In the case of programming languages, a programming language can also be used in a variety of programming paradigms.  2 Process-oriented:"process oriented" (Procedure oriented) is a process-centric programming idea. "Process-oriented" can also be called "record-oriented" programming ideas, they do not support rich "object-oriented" features (such as inheritance, polymorphism), and they do not allow the mixing of persistent state and domain logic. is to analyze the steps required to solve the problem, and then use the function to implement the step by step, the use of a one-time call in turn. Process is actually the most practical way of thinking, Even an object-oriented approach is a process-oriented idea. It can be said that the process-oriented approach is a basic method. It is considered to be practical. The general process is the refinement from the top down. So the process is the most important is the modular way of thinking. In contrast to process oriented, object oriented approach is mainly to object , the object includes properties and behavior. When the program size is not very large, the process-oriented approach will also show an advantage, because the process of the program is very clear, according to the module and function of the method can be well organized. 3 Object-oriented (OOP):Object-oriented programming (object Oriented Programming,oop, OO programming) is a computer programming architecture. One of the basic tenets of OOP is that a computer program is composed of a single unit or object that can act as a subroutine. OOP achieves the three main goals of software engineering: reusability, flexibility, and extensibility.    To achieve the overall operation, each object can receive information, process data, and send information to other objects.   Ii. Object-oriented benefits before object-oriented appearance, structured programming is the mainstream of program design, and structured programming is called process-oriented programming. In process-oriented programming, the problem is seen as a series of tasks that need to be done, functions (in this case, functions, procedures) to accomplish these tasks, and the focus of solving the problem is on functions. Where the function is process-oriented, that is, it focuses on how to accomplish the specified task according to the prescribed conditions. In a multi-function program, many important data are placed in the global data area so that they can be accessed by all functions. Each function can have its own local data. This structure can easily cause the global data to be inadvertently changed by other functions, so the correctness of the program is not easy to guarantee. One of the starting points of object-oriented programming is to make up for some drawbacks in process-oriented programming: objects are the basic elements of a program that tightly link data and operations and protect data from accidental changes by outside functions. comparing object-oriented programming and process-oriented programming, there are other advantages of object-oriented programming:1) The concept of data abstraction can change the internal implementation while keeping the external interface unchanged, thus reducing or even avoiding interference to the outside world;2) Through inheritance greatly reduce redundant code, and can easily extend the existing code, improve coding efficiency, but also reduce the error probability, reduce the difficulty of software maintenance;3) Combining object-oriented analysis and object-oriented design, it is possible to map the object in the problem domain directly into the program, and reduce the process of the intermediate link in the software development process;4) by distinguishing and dividing the software system into some relative independent parts, it is more convenient to control the complexity of software in a certain degree;5) Object-centric design can help developers to grasp the problem from static (attribute) and dynamic (method) Two aspects, so as to realize the system better;6) through the aggregation and Union of objects, we can realize the expansion of object's intrinsic structure and external function under the principle of encapsulation and abstraction, so as to upgrade the object from low to high.    Three , object-oriented three major features:1, encapsulation (sometimes referred to as information hiding) is to combine the data and behavior in a package, and the object of the user to hide the implementation of the data. Information hiding is the basic principle of object-oriented programming, and encapsulation is a way to realize this principle. Encapsulation enables objects to present a "black box" feature, which is a key step in the reuse and reliability of objects. 2. The idea of inheriting inheritance is to allow new classes to be built on the basis of existing classes. A subclass can inherit all members of the parent class, including properties and methods. The primary role of inheritance is to accomplish code reuse by implementing inheritance, and the completion of code reuse through interface inheritance. Inheritance is a technique of specification, not a technique of implementation. 3. Polymorphic polymorphism provides "interface and realization separation". Polymorphism can not only improve the structure and readability of the program, but also develop the "expandable" program. Inheritance is the basis of polymorphism. Polymorphism is the purpose of inheritance. It can enhance the simplicity, flexibility, maintainability, reusability and Expansibility of the program by using polymorphism based on class inheritance, multi-state based on interface inheritance, and polymorphism based on template. Four, the important concept of object-oriented1. Objects (methods)       Everything in the world is an object. The abstract mechanism of object-oriented program design is to abstract the problem to be solved as an object in object-oriented program. Use encapsulation to make each object have an individual identity. The program is a pile of objects, passing through the message, requesting other objects to work.  2, class   Each object is an entity in its class. Birds of a feather--that is, the class is a collection of similar objects. The object in the class can accept the same message. In other words: The class contains and describes a set of objects that have common attributes (data elements) and common behaviors (functions). For example: apples, pears, oranges and other objects belong to the fruit category.  3, methods       methods determine what kind of message an object can accept. Object-oriented design is sometimes simply summed up as "sending messages to objects". 4, interface       Each object has an interface. An interface is not a class, but a set of specifications for a class that conforms to the requirements of the interface. The interface describes what the class should do but does not specify how it should be done. A class can have one or more interfaces.  5, constructors       is used to initialize the content part state of a class, Python provides a constructor-style __init__ (); the __init__ () method is optional, and if not provided, Python gives the default __init_ _ Method General Data acquisition requires a defined get and set method. If there is no __init__ method function in a class, the instance object created by the class name is empty, the tangent is not initialized, and if there is this method function, it is usually the first method function of the class, somewhat like a constructor in a language such as C + +. —————————————————————————————————————— class Ca:        def __init__ (self , V): # Note the front and back two underscores                  self.name = V        def pr (self):       &nbsp         print "A--->", Self.nameia = Ca ("jeapedu") # essentially called __init_ _ Method Function Ia.pr () CA.PR (IA) —————————————————————————————————————— output a---> Jeapedua---> jeapedu6, destructors       is used to free the resources occupied by the object, the destructor provided by Python is __del__ (); __del__ () is optional, and if not provided, Python provides default destructors in the background if you want to explicitly call a destructor, You can use the DEL keyword as follows: Del object name 7, private method, private property       Python default member functions and member variables are public and are not decorated with keywords such as public,private in other languages. Defining a private variable in Python only needs to precede the variable or function name with the "__" two underscore, then the function or variable will be private. Internally, Python uses a name mangling technology to replace __membername with _classname__membername, so you'll be prompted to find it when you use the name of the original private member externally. For example: #!/usr/bin/env python
#Author is Wspikh
#-*-Coding:encoding-*-
class Person (object):

def __init__ (self):
Self.__name = ' haha ' #私有属性
Self.age = 22

def __get_name (self,old): # #私有方法
#return Self.__name
Print ("%s" is an expression, not the mood, but to the age of%d! "% (Self.__name,old))

def pos (self):
Self.__get_name (55)

def get_age (self):
return Self.age #返回特定的值

person = person () #实例化
Print (Person.get_age ()) #打印特定的值
#print (Person.__get_name ()) #报错, private access
Person.pos () #直接调用pos () method The result is: 22haha is a kind of expression, and the mood has nothing to do, but by the age of 55 can! The __name we define here is a private property, and __get_name () is a private method. If you visit directly, you will be prompted not to find the relevant properties or methods, but if you really want to access the private data, it is also accessible, strictly speaking, private methods are accessible outside their classes, but not easy to handle. Nothing in Python is truly private; internally, the names of private methods and properties are suddenly changed and restored so that they appear to be unusable with their given names 8, class variables, instance variables class variables:is a value that can be shared among all instances of a class (that is, they are not assigned individually to each instance). For example, in the following example, Num_of_instance is a class variable that tracks how many instances of test exist. instance variable:Once instantiated, each instance has a separate variable.

#测试类变量和实例变量

Class Test (object):
num_of_instance = 0 #类变量
def __init__ (self, name):
Self.name = Name
Test.num_of_instance + = 1

if __name__ = = ' __main__ ':
Print (test.num_of_instance)
T1 = Test (' Jack ')
Print (test.num_of_instance)
T2 = Test (' Lucy ')
Print (T1.name, t1.num_of_instance)
Print (T2.name, t2.num_of_instance) results as follows: 01jack 2lucy 2 v. Add:1, "As long as the object, there must be a category, as long as the object, there must be a property of" 2, "instantiation is the initialization of a class, created an object. The process of turning a class into a concrete object "3," a class variable is a common attribute in a class, saving overhead. "4," the destructor is executed at the time of instance, destruction, usually used to do some finishing work, such as closing some database connections, open temporary files Six, thinking:1 subclasses to add a variable, you must refer to the parent class's constructor, such as DEF CS (object): Def __init__ (self,name,age) # print ("This is Fuel") def cos (CS): def __init__ (Self,name,age,hobby) cs.__init__ (self,name,age) #print ("This is Zilei") COS1 = Co S ("X", 18,money) print (cos1.hobby) 2 breadth First (PYTHON3) Py2 Classic class depth First new class breadth first Py3 Classic class and new class are breadth first 3 polymorphism: one interface, reuse

The ordinary way of Python (6)

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.