1, Create classes and objects
Object-oriented programming is a kind of programming method. The landing of this programming method needs to use "class" and "object". Therefore, object-oriented programming is actually the use of "class" and "object".
Class is a template. The template can contain multiple functions, and some functions can be implemented in the functions
Object is an instance created according to the template, through which functions in the class can be executed
Class is a keyword, representing a class
To create an object, add parentheses after the class name
Example:
# Create class class Foo: def Bar(self): print 'Bar' def Hello(self, name): print 'i am %s' %name # Create object obj according to class Foo obj = Foo() obj.Bar() #Execute Bar method obj.Hello('wupeiqi') #Execute Hello method
Hey, do you have any questions here? Using function programming and object-oriented programming to execute a method is easier than object-oriented programming
Object oriented: [create object] [implement method through object]
Function programming: [execute function]
Observe that the above comparison answer is yes, and then it is not absolute. Different scenarios are suitable for different programming methods.
Summary: functional application scenarios -- > each function is independent and has no shared data
2, Three characteristics of object-oriented
1. encapsulation
As the name implies, it is to encapsulate the content in a certain place, and then call the content encapsulated in a certain place later.
Therefore, when using object-oriented encapsulation features, you need to:
Encapsulate content somewhere
Call encapsulated content from somewhere
Step 1: encapsulate content somewhere
Self is a formal parameter, when obg1 = foo ('wupeiqi ', 18) is executed, self is equal to obg1
When obb2 = foo ('alex ', 78) is executed, self is equal to obb2
Therefore, the content is actually encapsulated in obj 1 and obj 2. Each object has name and age attributes, which are stored in memory similar to the following figure.
The distribution of objects and classes in memory is as follows:
Step 2: call the encapsulated content from somewhere
When invoking encapsulated content, there are two situations:
- Call directly through object
- Call indirectly through self
1. Calling the encapsulated content directly through the object
The figure above shows the way that objects obp1 and obp2 are saved in memory. According to the saving format, you can call the encapsulated content: object. Attribute name
class Foo: def __init__(self, name, age): self.name = name self.age = age obj1 = Foo('wupeiqi', 18) print obj1.name # Call the name property of obj 1 object directly print obj1.age # Calling the age property of obj 1 object directly obj2 = Foo('alex', 73) print obj2.name # Call the name property of obj2 object directly print obj2.age # Calling the age property of obj 2 object directly
2. Indirectly call the encapsulated content through self
When executing a method in a class, you need to call the encapsulated content indirectly through self
class Foo: def __init__(self, name, age): self.name = name self.age = age def detail(self): print self.name print self.age obj1 = Foo('wupeiqi', 18) obj1.detail() # By default, Python will pass obj 1 to self parameter, namely obj 1.detail (obj 1). Therefore, self = obj 1 in the method, that is, self.name is wupeiqi; self.age is 18 obj2 = Foo('alex', 73) obj2.detail() # By default, Python will pass obj2 to the self parameter, that is, obg1.detail (obb2). Therefore, at this time, self = obb2 in the method, that is, self.name is alex, self.age is 78
2. inheritance
Inheritance, object-oriented inheritance and real-life inheritance are the same, that is, the child can inherit the content of the parent.
For example:
Cats can: meow, eat, drink, pull and scatter
Dogs can bark, eat, drink, pull and scatter
If we want to create a class for the cat and the dog, we need to implement all their functions for the cat and the dog, as follows:
class Cat: def meow(self): print 'meow' def eat(self): # do something def drink(self): # do something def PULL(self): # do something def Scatter(self): # do something class Dog: def Bark(self): print 'meow' def eat(self): # do something def drink(self): # do something def PULL(self): # do something def Scatter(self): # do something //Pseudo code
It's not hard to see that eating, drinking, pulling and scattering are the functions of both cats and dogs, but we have written them twice in our cat and dog classes respectively. If the idea of inheritance is used, it is implemented as follows:
Animals: eat, drink, pull and scatter
Cat: meow (cat inherits animal function)
Dogs: Barking (dogs inherit the function of animals)
class Animal: def eat(self): # do something def drink(self): # do something def PULL(self): # do something def Scatter(self): # do something # Write another class name in parentheses after the class, indicating that the current class inherits another class class cat(Animal): def meow(self): print 'meow' # Write another class name in parentheses after the class, indicating that the current class inherits another class class Dog(Animal): def Bark(self): print 'meow' //Copy code
class Animal: def eat(self): print "%s eat " %self.name def drink(self): print "%s drink " %self.name def shit(self): print "%s PULL " %self.name def pee(self): print "%s Scatter " %self.name class Cat(Animal): def __init__(self, name): self.name = name self.breed = 'cat' def cry(self): print 'meow' class Dog(Animal): def __init__(self, name): self.name = name self.breed = 'Dog' def cry(self): print 'Bark' # ######### implement ######### c1 = Cat('The little black cat of Xiaobai's family') c1.eat() c2 = Cat('Little black little white cat') c2.drink() d1 = Dog('Fat family's skinny dog') d1.eat()
Therefore, for object-oriented inheritance, it is actually to extract the methods shared by multiple classes into the parent class, and the child class only needs to inherit the parent class without implementing each method one by one.
Note: in addition to the names of subclasses and superclasses, you may have seen derived classes and base classes, which are only named differently from subclasses and superclasses.
After learning the writing method of inheritance, we use the code to describe the above functions of the cat and dog:
class Animal: def eat(self): print "%s eat " %self.name def drink(self): print "%s drink " %self.name def shit(self): print "%s PULL " %self.name def pee(self): print "%s Scatter " %self.name class Cat(Animal): def __init__(self, name): self.name = name self.breed = 'cat' def cry(self): print 'meow' class Dog(Animal): def __init__(self, name): self.name = name self.breed = 'Dog' def cry(self): print 'Bark' # ######### implement ######### c1 = Cat('The little black cat of Xiaobai's family') c1.eat() c2 = Cat('Little black little white cat') c2.drink() d1 = Dog('Fat family's skinny dog') d1.eat()
So the question is coming again. How about more inheritance?
Whether multiple classes can be inherited
If you inherit multiple classes and each class has the same function, which one will be used?
1. Python's classes can inherit multiple classes, while Java and C ා can only inherit one class
2. If Python classes inherit multiple classes, there are two ways to find methods: depth first and breadth first
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 it. If it is not found in class A, continue to find it in class B. if it is not found in class B, continue to find it in class D. if it is not found in class D, continue to find it in class C. if it is still not found, an error will be reported # So, the search order: a -- > B -- > D -- > C # In the above process of finding bar method, once it is found, the search process will be interrupted immediately and the search will not continue a.bar() //Classic class multi inheritance
class D(object): 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 search. If there is no class A, continue to search in class B. if there is no class B, continue to search in class C. if there is no class C, continue to search in class D. if there is no class C, an error will be reported # Therefore, the search order is: A -- > B -- > C -- > D # In the above process of finding bar method, once it is found, the search process will be interrupted immediately and the search will not continue a.bar() //New class multi inheritance
Classic class: first, go to class A to find it. If it is not found in class A, continue to find it in class B. if it is not found in class B, continue to find it in class D. if it is not found in class D, continue to find it in class C. if it is still not found, an error will be reported
New class: first, go to class A to find it. If it is not found in class A, continue to find it in class B. if it is not found in class B, continue to find it in class C. if it is not found in class C, continue to find it in class D. if it is still not found, an error will be reported
Classic class and new class are mainly to see whether there is intersection at the top of multi inheritance
Note: in the above search process, once found, the search process will be interrupted immediately, and the search will not continue
3. polymorphism
Python doesn't support polymorphic writing in strongly typed languages such as Java and C ා, but it's native polymorphic, and python advocates "duck type".
class F1: pass class S1(F1): def show(self): print 'S1.show' class S2(F1): def show(self): print 'S2.show' # Due to Java or C#When defining a function parameter in, you must specify the type of the parameter # In order for Func function to execute show method of S1 object and show method of S2 object, a parent class of S1 and S2 classes is defined # The actual parameters passed in are: S1 object and S2 object def Func(F1 obj): """Func Function needs to receive a F1 Type or F1 Type of subclass""" print obj.show() s1_obj = S1() Func(s1_obj) # In the Func function, pass in S1 obj, the object of S1 class, and execute show method of S1. Result: S1.show s2_obj = S2() Func(s2_obj) # In the Func function, pass in the Ss obj object of the Ss class, and execute the show method of the Ss. Result: S2.show Python Pseudo code implementation Java or C#Polymorphism
class F1: pass class S1(F1): def show(self): print 'S1.show' class S2(F1): def show(self): print 'S2.show' def Func(obj): print obj.show() s1_obj = S1() Func(s1_obj) s2_obj = S2() Func(s2_obj) Python "Duck type "
Object oriented programming is a kind of programming method, which is based on the use of classes and objects
Class is a template with multiple "functions" wrapped for use
Object, an instance created from a template (that is, an object) that is used to call a function wrapped in a class
Three characteristics of object-oriented: encapsulation, inheritance and polymorphism