Thanks to the guidance of our predecessors, http://www.cnblogs.com/yupeng/p/3414736.html
Http://www.cnblogs.com/alex3714/articles/5188179.html
Http://www.cnblogs.com/wupeiqi/p/4493506.html
Depth First: https://www.cnblogs.com/yupeng/p/3414736.html
Breadth First: https://www.cnblogs.com/zhaof/p/7092400.html
Individuals to summarize the advantages of the five blog, and with respect to the elders, the following are pure hand, the code has been verified
Can be run in python3.6.3
Object-oriented is a programming method that requires classes and objects to be implemented, and object-oriented programming is actually the use of classes and objects
Class refers to a template, the function of the template can have more than one function to implement some functions
Object creates an instance from a template, executes a function in a class from an instance object
Object-oriented; "Create Object" "Execute method by Object"
Class Foo:
def Bar (self):
Print ("Bar")
def Hello (self,name):
Print ("I am%s"% (name))
Obj=foo ()
Obj. Bar ()
Obj. Hello ("Wupeiqi")
Object-oriented three main features: encapsulation, inheritance, polymorphism
One, encapsulation: Put the content somewhere, and later, if necessary, you can call the content with some kind of function
The first step: encapsulate the content somewhere:
Step two: Call the encapsulated content from somewhere
The first step is detailed:
Class Foo:
def __init__ (self,name,age):
Self.name=name
Self.age=age
#根据类对象Foo创建对象
#自动执行Foo类的__init__方法
Obj1=foo (' mad ') #将狂和18分别封装到name和age属性中
The second step is detailed:
To invoke a related function from somewhere:
There are two scenarios (attributes in all languages)
1, called directly through the object
2, indirect invocation via self
Let's start with the first
Class Foo:
def __init__ (self,name,age):
Self.name=name
Self.age=age
Obj1=foo (' Wupeiqi ', 18)
Print (Obj1.name)
Print (Obj1.age)
Obj2=foo (' Alex ', 73)
Print (Obj2.name)
Print (Obj2.age)
The second kind of introduction:
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 ()
Obj2=foo (' Alex ', 73)
Obj2.detail ()
Thus, the second indirect call through self refers to a function to access
Practice One : output The following information at the terminal
Xiao Ming, 10 years old, male, go up the hill to chop Wood
Xiao Ming, 10 years old, male, drive to northeast
Xiao Ming, 10 years old, male, favorite big health care
Lao Li, 90-year-old man, go up the hill to chop Wood
Lao Li, 90-year-old, male, drive to Tohoku
Lao Li, 90 years old, male, favorite big health care
Class Foo:
def __init__ (self,name,age,sex):
Self.name=name
Self.age=age
Self.sex=sex
def Kanchai (self):
Print ("%s,%s years old,%s, go up the hill to chop Wood"% (Self.name, self.age, Self.sex))
def qudongbei (self):
Print ("%s,%s years,%s, drive to Tohoku"% (Self.name, self.age, Self.sex))
def Dabaojian (self):
Print ("%s,%s years old,%s, favorite big health"% (Self.name, self.age, Self.sex))
Xiaoming = Foo (' xiaoming ', 10, ' male ')
Xiaoming.kanchai ()
Xiaoming.qudongbei ()
Xiaoming.dabaojian ()
Laoli = Foo (' Lao Li ', 90, ' Male ')
Laoli.kanchai ()
Laoli.qudongbei ()
Laoli.dabaojian ()
Second, Inherit:
Inheritance, the inheritance in object-oriented is the same as the inheritance in real life, that is, the child can inherit the parent's content
For example, people, students, workers
Students and workers are people's two extension subclasses
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)
def pee (self):
Print ('%s scatter '%self.name)
Class Cat (Animal):
def __init__ (self,name):
Self.name=name
Self.bread= ' Cat '
Def cry (self):
Print (' Meow meow ')
Class Dog (Animal):
def __init__ (self, name):
Self.name = Name
Self.bread = ' dog '
Def cry (self):
Print (' barking ')
C1 = Cat (' Small White House ' little Black Cat ')
C1.eat ()
C2 = Cat (' Little black White Cat ')
C2.drink ()
D1 = Dog (' Fat family's little skinny Dog ')
D1.eat ()
Therefore, for object-oriented inheritance, it is actually the method of extracting multiple classes common to the parent class, the subclass only inherits the parent class and does not need to implement each method
So a very crucial question is, how do we prepare for multiple inheritance?
Whether multiple classes can be inherited
If you inherit more than one class and define the same function in each class, which one will be used?
1. Python classes can inherit multiple classes, and Java and C # can inherit only one class
2. If the Python class inherits more than one class, there are two ways to find the method: depth first and breadth first
The depth-first and breadth-first details are given in the Appendix:
When a class is a classic class, multiple inheritance cases are searched in the depth-first way
When a class is a new class, multiple inheritance cases are searched in terms of breadth first
Distinction: The current class or parent inherits the object class, which is the new class, otherwise the classic class
Class C1: #C1是经典类
Pass
Class C2 (object) #C1是经典类
Pass
Class C1: #C1是新式类
Pass
Class C2 (C1) #C1是新式类
Pass
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 ()
A.bar ()
# Execution Bar Method # first go to a class to find, if not in Class A, 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, the error # so, the search order: A --> B --> C --> D# in the process of finding the bar method above, once found, the search process is immediately interrupted, Will not continue to find the classic class: First go to a class to find, if not in Class A, then continue to find in class B, 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 the new class: first go to Class A to find, if not in Class A, 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 Class animal (object): def __init__ (Self, name): # constructor of the class self.name = name def talk (self): # Abstract method, defined by convention only raise notimplementederror ("subclass must implement abstract Method ") Class cat (Animal): def talk (self): print ('%s: meow meow! ') % sElf.name) Class dog (Animal): def talk (self): print ('%s: Wang! Wang! Wang! ' % self.name) def func (obj): # one interface, multiple morphology obj.talk () c1 = cat (' Xiao Qing ') d1 = dog (' Li Lei ') func (C1) func (d1) The correct code func function needs to receive a F1 type or F1 subclass of type "" " obj.show () s1_obj = s1 () func (s1_obj) # objects passed into the S1 class in the Func function s1_obj, execute  S1 The show method of , Result: S1.shows2_obj = s2 () func (s2_obj) # passed in the Func function to the object ss_obj of the SS class, execution ss Show method, Result: s2.show copy Code class f1: passclass s1 (F1): def show (self): print ' S1.show ' Class s2 (F1): def show (self): print ' S2.show ' # Because you must specify the type of the parameter when defining a function parameter in Java or C # # in order for the Func function to execute the Show method of the S1 object, and to execute the Show method of the 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 subclass type" "" Print (Obj.show ()) s1_obj = s1 () func (s1_obj) # objects passed into the S1 class in the Func function s1_obj, execute  S1 The show method of , results: S1.shows2_obj = s2 () func (s2_obj) # passed in the Func function to the object ss_obj of the SS class, performing Ss Show method, Result: S2.show error code, version wrong
Depth first and breadth priority will be applied in many areas such as crawlers
The first explanation:
Depth-First algorithm:
(1) accesses the initial vertex V and marks the vertex v has been accessed.
(2) finds the first adjacency vertex W of Vertex v.
(3) if the adjacency vertex W of Vertex v is present, continue execution; otherwise back to V, Find another contiguous point of V that has not been visited.
(4) If Vertex W has not been accessed, access the vertex W and mark the vertex w as accessed.
(5) continue to find the next adjacent vertex of vertex W Sub style= "margin:0px;padding:0px;" >i
breadth first algorithm:
(1) vertex v into the queue.
(2) Continue execution when the queue is non-empty, otherwise the algorithm ends.
(3) out of the queue to get the team overhead point v; Access Vertex v and Mark Vertex v has been accessed.
(4) finds the first adjacency vertex of Vertex v Col.
(5) Col is queued if the adjacent vertex of V Col is not visited.
(6) continue to find another new contiguous vertex of Vertex v Col, Go to Step (5). Until all the unreachable adjacency points of Vertex v have been processed. Go to step (2).
The second explanation:
Depth first refers to the web crawler from the start page, a link to track down a link, after processing the line and then into the next Start page, continue to follow the link, through understanding:
Note: The scrapy default is a depth-first algorithm
650) this.width=650; "Src=" Https://s3.51cto.com/oss/201711/08/117ff53d4779f3aeb01aeecfaeb73ca8.png-wh_500x0-wm_3 -wmp_4-s_1760643635.png "title=" depth first. png "alt=" 117ff53d4779f3aeb01aeecfaeb73ca8.png-wh_ "/>
650) this.width=650; "Src=" Https://s1.51cto.com/oss/201711/08/a1f0533024bf9b6546cfba343b426558.png-wh_500x0-wm_3 -wmp_4-s_3626293800.png "title=" 1.png "alt=" A1f0533024bf9b6546cfba343b426558.png-wh_ "/>
A-b-d-e-i-c-f-g-h (Recursive implementation)
Breadth first, someone also called width first, refers to the new download page Discovery link directly to the end of the URL queue to crawl, that is, the web crawler will first crawl all pages in the Start page, and then select one of the connection pages, continue to crawl all the pages linked in this page, by understanding: 650) this.width=650; "Src=" https://s3.51cto.com/oss/201711/08/0be45ed3d6f52f2752de58bac131c3e7.png-wh_500x0-wm_3-wmp _4-s_3100239767.png "title=" 2.png "alt=" 0be45ed3d6f52f2752de58bac131c3e7.png-wh_ "/>
A-b-c-d-e-f-g-h-i (Queue Implementation)
Python Object-oriented basics