Refer to Object-oriented, always inseparable from a number of important terms: polymorphism (polymorphism), Inheritance (inheritance) and encapsulation (encapsulation)
Defining a class can be defined in the following way:
class ClassName: # defining functions using Def to define classes also requires class block
Note that there is a colon behind the class name, and you can define the properties and methods within the block blocks. When a class is defined, a class object is created. The class object supports two operations: reference and instantiation. A reference operation invokes a property or method in a class through a class object, and instantiation is an instance of a class object called an instance object. For example, define a people class:
class people: ' Jack ' # defines a property # defines a method def printname (self): # defining a function inside a class is called a method. Print self.name
When the People class definition is complete, a global class object is created, and the properties and methods in the class can be accessed through the class object. The instantiation operation, P=people (), produces a people instance object, which can also be accessed by the instance object P (p.name), where p is the object of the People
Name is a property, and Printname () is a method that is called as a method for binding a function to an object. Functions that are typically defined within a class are bound to class objects or instance objects, so called as methods, and functions defined outside of the class are generally not bound to the object, called functions.
In a class, you can define methods as needed, define methods with the DEF keyword, a method defined in a class has at least one parameter, and the default is to use a variable named ' self ' as the parameter (with a different name), and it needs to be the first argument. Let's look at an example:
class people: ' Jack ' = def GetName (self): return self.name def getage (self): return = people ()print(P.getname (), P.getage ())
Construction method
There are some built-in methods in Python that have a special place for naming them (the method name starts with 2 underscores and ends with 2 underscores)
__init__ (self,....) Called when the object is generated, can be used to do some initialization operations, do not need to display the call, the system will be executed by default. The construction method supports overloading, and the default construction method is automatically executed if the user does not redefine the constructor itself.
class Vector: def __init__ (self, A, b): = a = b
Inherited:
Inheritance in object-oriented is the same as inheritance in real life, that is, the child can inherit the parent's content.
#---------------Inheritance-----------------classAnimals:defChi (self):Print(Self.name +"\ t Eat") defHe (self):Print(Self.name +"\ t Drink") defPiao (self):Print("Prostitute-1") classUncle:defdu (self):Print("Gambling") defPiao (self):Print("Prostitute-2") classDog (animals,uncle):def __init__(self,name): Self.name=namedefJiao (self):Print(Self.name +"\ \ Wang Bark ~") Alex= Dog ("Li Jie") Alex.piao ()
In the case of object-oriented inheritance, it is actually the method of extracting the common methods of multiple classes into the parent class, in which the subclass inherits only the parent class without having to implement each method.
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.
#Classic class <br>class D: defBar (self):Print 'D.bar' classC (D):defBar (self):Print 'C.bar' classB (D):defBar (self):Print 'B.bar' classA (B, C):defBar (self):Print 'A.bar'a=A ()#when you execute the 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 D, if there is a class D, then continue to the C class to find, if still not found, the error#So, look in order: A-and B--and D-and C#In the process of finding the bar method above, once found, the search process is immediately interrupted and no further searchA.bar ()
#New Class classD (object):defBar (self):Print 'D.bar' classC (D):defBar (self):Print 'C.bar' classB (D):defBar (self):Print 'B.bar' classA (B, C):defBar (self):Print 'A.bar'a=A ()#when you execute the 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, look in order: A--B --and C--D#In the process of finding the bar method above, once found, the search process is immediately interrupted and no further searchA.bar ()
Python basic article "seventh" object-oriented (top)