Object-oriented programming abbreviation OOP (oop,object-oriented programming) is a programming idea, OOP takes object as the basic unit of program, and an object contains functions of data and manipulation data.
The object-oriented design abbreviation OOD (ood,object-oriented Designer) OOD only means to create a system that you create using an object-oriented approach to architecture.
Process-oriented programming treats a computer program as a set of commands, which is the sequential execution of a set of functions. In order to simplify the program design, the process will continue to cut the function into sub-function, that is, the large function by cutting into small block function to reduce the complexity of the system.
Object-oriented programming treats a computer program as a set of objects, and each object can receive messages from other objects and process them, and the execution of a computer program is a series of messages passing between objects.
In Python, all data types can be treated as objects and, of course, objects can be customized. The custom object data type is the concept of class in object-oriented.
commonly used terminology for objects:
1. Class:
A collection of objects that describe the same properties and methods. It defines the properties and methods that are common to each object in the collection. An object is an instance of a class.
2. Class variable: The
class variable is common in the entire instantiated object. Class variables are defined in the class and outside the body of the function. Class variables are not typically used as instance variables.
3. Data members:
class variables or instance variables are used to manipulate the data related to the class and its instance objects.
4. Method overrides:
If the method inherited from the parent class does not meet the requirements of the subclass, it can be overridden, which is called the override of the method, also known as the override of the method.
5. Instance variable:
defines a variable in a method that acts only on the class of the current instance.
6. Inheritance:
is a derived class (derived class) that inherits the fields and methods of the base class. Inheritance also allows the object of a derived class to be treated as a base class object. For example, there is a design where an object of type dog is derived from the animal class, which is the analog "is a (is-a)" Relationship (example, dog is a anim al).
7. Instantiation:
Creates an instance of a class, the concrete object of the class.
8. Method: The function defined in the
class.
9. Object:
An instance of a data structure defined by a class. The object consists of two data members (class variables and instance variables) and methods.
Python joins the class mechanism when possible without adding new syntax and semantics, compared to other programming languages. The classes in
Python provide all the basic functions of object-oriented programming: The inheritance mechanism of classes allows for multiple base classes, and derived classes can overwrite any method in the base class, which can call a method of the same name in the base class. The
object can contain any number and type of data. Comprehensive representation of the
class:
Class Classname_1: #定义类 def __init__ (self, NM, ph): #定义构造器 SELF.NM = NM #设置名字 self.ph = ph print (' self-directed memory address: ', self) print (self.nm) print (self.ph) def def _name_1 (self, newph): #定义方法 self.nm = self.nm self.ph = newph #重新命名 Print (' method self ' points to memory address: ', self ') print (self.nm) print (' New phone number: ', self.ph) class classname_2 (Classname_1): #创建子类 def __init__ (self, NM, ph, id, EM): Super (classname_2, self). __INIT__ (nm, ph) self.id = ID Self.em = em print (' The memory address that the ' class self points to: ', self) # print (SELF.NM) # print (self.ph) print (self.id ) Print (SELF.EM) def def_name_2 (self, newemail): self.id = self.id Self.em = newemail Print (' The memory address that the class self points to: ', self) # print (SELF.NM) # print (self.ph) print (self.id) print (SELF.EM)
Result of Operation:
/library/frameworks/python.framework/versions/3.5/bin/python3.5/users/ Zk/pycharmprojects/old_boy/week06/class_ object-oriented. Py class self points to the memory address: <__main__. Classname_1 object at 0x101b84208> xiaoming 110 instantiates the memory address that ret_2 points to: <__main__. Classname_1 object at 0x101b84208> method self points to the memory address: <__main__. Classname_1 object at 0x101b84208> Ming: Class 110 The memory address that the self points to: <__main__. Classname_2 object at 0x101b842e8> Red 119 self points to the memory address: <__main__. Classname_2 object at 0x101b842e8>66[email protected] instantiates the memory address that ret_2 points to: <__main__. Classname_2 object at 0x101b842e8> class self points to the memory address: <__main__. Classname_2 object at 0x101b842e8>66[email protected] method self points to the memory address: <__main__. Classname_2 object at 0x101b842e8> red new phone number: 1233333333333333333Process finished with exit code 0
The proprietary methods of the class:
__init__: constructor, called when generating an object
__DEL__: Destructors, using when releasing objects
__REPR__: printing, converting
__SETITEM__: Assigning values by index
__getitem__: Getting values by index
__LEN__: Get length
__CMP__: comparison operation
__CALL__: Function call
__ADD__: Add operation
__sub__: minus operation
__MUL__: Multiply operation
__DIV__: Except operations
__mod__: Finding the remainder operation
__POW__: exponentiation
Operator Overloading:
Class Vector: def __init__ (self, A, b): SELF.A = a self.b = b def __str__ (self): print (SELF.A, Type (SELF.A)) print (Self.b,type (self.b)) return ' Vector (%d,%d) '% (SELF.A, self.b) def __add__ (self, Other): print (Self.a,type (SELF.A)) print (Self.b,type (self.b)) return Vector (SELF.A + OTHER.A, self.b + other.b) v1 = vector (2, ten) v2 = vector (5,-2) print (V1 + v2)
Result of Operation:
Vector (7, 8)
Object-oriented features: encapsulation. Inheritance. Polymorphism
Packaging:
As the name implies, encapsulate the content somewhere, and then call 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
#----------------Package----------------class Person: def __init__ (self,name,age,weight): #构造方法, automatically executes when an object is created Self . Name = name Self . Age = Age Self . Weight = Weight def chi (self): self . Weight = self. Weight + 2 print (self. Weight) def jianshen (self): self . Weight = self. Weight-1 print (self. Weight) # Create an object based on person # automatically executes the __init__ method of the Personl class O1 = person (' Xiao Ming ', 22,120) #将小明和22, 120 encapsulated into the Name,age,weight attribute respectively # Instantiate O1.jianshen () O1.chi ()
Operation Result:
119121
Inherited:
Inheritance in object-oriented is the same as inheritance in real life, that is, the child can inherit the parent's content.
#---------------inherit-----------------class Animals: def chi (self): print (Self.name + "t-eat") def He (self): Print (Self.name + "T-drink") def Piao (self): print ("prostitute-1") class Uncle: def du (self): print ("gambling") def piao (self): print ("Prostitute-2") class Dog (animals,uncle): def __init__ (self,name): self.name = Name def jiao (self): print (self.name + "\ t Wang Woo ~") Alex = Dog ("Li Jie") Alex.piao ()
Operation Result:
Prostitute-1
Therefore, for object-oriented inheritance, it is actually the method of extracting multiple classes common to the parent class, and the subclass inherits only the parent class without having to implement each method in one.
Note: In addition to the names of subclasses and parent classes, you may have seen derived and base classes that are only different from subclasses and parent classes.
Multiple inheritance:
Python classes can inherit multiple classes, and Java and C # can inherit only one class
If a Python class inherits more than one class, there are two ways to find it: depth first and breadth first
When a class is a classic class, multiple inheritance cases are searched in the depth-first way
When a class is a new class, in multiple inheritance cases, the breadth-first method is found
Classic class and new class, literally can see an old a new, new inevitably contain with many functions, is also recommended after the wording, from the wording of the words, if the current class or the parent class inherits the object class, then the class is a new class, otherwise it is the classic class.
#经典类
Class D: def bar (self): print ' D.bar ' class C (d): def Bar (self): print ' C.bar ' class B (d): def Bar ( Self): print ' B.bar ' class A (B, C): def Bar (self): print ' a.bar ' a = A () # when executing the Bar method # First go to Class A to find, if not in Class A, then go to class B to find , if there is a class B, then continue to find in Class D, if there is a class D, then continue to find in class C, if still not found, then error # So, look in order: A--and B----and C # in the process of finding the bar method, once found, the search process immediately interrupted, Will not continue to find the A.bar ()
#新式类class D (object): def Bar (self): print ' D.bar ' class C (d): def Bar (self): print ' C.bar ' class B (d): def bar: print ' B.bar ' Class A (B, C): def Bar (self): print ' a.bar ' a = A () # when executing the Bar method # First go to Class A to find, If the class A does not, then continue to find in class B, if there is a class B, then continue to find in Class C, if there is a Class C, then continue to find in class D, if still not found, then error # So, look in order: A----and C--and d# in the process of finding the bar method, Once found, the search process is interrupted immediately and no further a.bar () will be found.
Polymorphic:
Python does not support polymorphic
Private properties of the class
__private_attrs: Two underscores begin with, declaring that the property is private and cannot be used outside of the class or accessed directly. Self.__private_attrs when used in methods inside a class.
Methods of the class
Inside the class, using the DEF keyword, you can define a method for a class that differs from a generic function definition, which must contain the parameter self, and is the first argument
Private methods of the class
__private_method: Two underscores, declares that the method is a private method and cannot be called outside of a class. Calls Slef.__private_methods inside the class.
Python basic article "13th": Object-oriented