An understanding of object-oriented (OOP)
In human cognition, we are accustomed to classify things with similar attributes. So, the class xxx that we write is a category that describes a set of properties. And we refer to this set of attributes as concrete examples of objects
Like what:
Class Bird (object): Def __init__ (self,feather,call): Self.feather=feather self.call=call
We define a bird class, and the initialization function defines that it has feathers, which can be called. This is the class.
Bird=bird ("Yellow", "Jiujiu")
We have defined an object. Feathers of birds, chirping is tweeted
Of course, object-oriented is not that simple. Which is still related to the
Inherited
Multiple inheritance, superstitious inheritance,
Method Overrides,
permissions, such as whether private data from the parent class can be read and manipulated in subclasses
The way to implement inheritance in Python is to write the parent class directly in the parentheses of the class. For example, the parent class of bird is Object
About subclass Inheritance Parent class:
#!/usr/bin/env python#-*-coding:utf-8-*-class birds:def __init__ (self,name,feather,year): self.name= Nameself.feather=featherself._year=yeardef Call (self):p rint "The is a mether----for bird" def get_year (self): return Self._yearclass Littebird (Birds):p Assb=littebird ("ll", ' Yellow ', ' 100 ')
If pass is defined in the subclass, then all functions and arguments of the parent class will be received. All the parameters of a class in Python are defined in __init__ () and appear not to be defined directly outside the function. That feeling is strange, helpless Caishuxueqian. It's not good enough to fathom.
Access Restrictions:
__name: This form is represented in Python as a private data/method, and Python converts it to _classname__name
__NAME__: This is the one in Python that represents a special variable.
_name: Although this can be accessed, but indicates that I am private data, it is best not to access
Therefore, it is _fathername__name to make private in the parent class. When you want to access the private method of the parent class in the subclass, or the data is, you will be prompted that the _sonname__name does not exist
Therefore, you directly call Self.__year directly in the subclass is not present. Because it was changed into a __sonname__year. Only if you change to the _fathername__year of the parent class can you access
#!/usr/bin/env python#-*-coding:utf-8-*-class birds:def __init__ (self,name,feather): self.name=nameself.feather= Featherself.__year=100def Call (self):p rint "The is a mether----for bird" def __get_year (self):p rint Self.__yearclass Littebird (Birds):d ef set_year (self):p rint self.__year #找不到. B=littebird ("ll", ' Yellow ') b.set_year ()
Change to the following to access the data:
#!/usr/bin/env python#-*-coding:utf-8-*-class birds:def __init__ (self,name,feather): self.name=nameself.feather= Featherself.__year=100def Call (self):p rint "The is a mether----for bird" def __get_year (self):p rint Self.__yearclass Littebird (Birds):d ef set_year (self):p rint Self._birds__yearb=littebird ("ll", ' Yellow ') b._birds__get_year ()
So about access restrictions:
The data that is set to private in a class. will be changed into a straightforward way of _classname__name. So it is not directly accessible in subclasses. Unless you change it into a straightforward way.
Polymorphic:
On the understanding of polymorphism. Is that we can override the parent class function. Like what
B=litterbird ()
There are no emphasis types in Python. However, the B in this case is both the birds type and the Litterbird type. Can be seen with isinstance (). So. We can then define our own methods in subclasses. Reusing common methods in a parent class
Multiple inheritance and superstition inheritance:
Multiple inheritance is the inheritance of multiple classes at the same time. It's going to be faster.
Superstition is mixed with inheritance, which belongs to dynamic multi-inheritance. will be slow
One point, multi-inheritance if, the inherited two classes have the same method name? Then execute the method that is in the front line.
If subclasses have methods, execute subclass methods
If the subclass does not have a method, execute the parent class method
If more than one parent class is executed, the first parent class
If the first parent class does not, but the parent class of the parent class has, will the parent class of the parent class be executed, or the next parent class?
Executes the parent class of the parent class
#!/usr/bin/env python#-*-coding:utf-8-*-class grandfather:def hair (self):p rint "Ghair" class mother (Grandfather): Passclass father:def hair (self):p rint "No Hair" class son (Mother,father):p Asss=son () S.hair () #Ghair
This article from "ehealth" blog, declined reprint!
About object-oriented in Python