Three days do not practice layman, the most important thing is to insist!
Wu Sir's blog is very clear, the key is that the class is very good, once again understand, the original address: http://www.cnblogs.com/wupeiqi/p/4493506.html
Process oriented: base code from top to bottom according to business logic
Functional programming: Edit different functions according to different functional requirements, call the function when using, a function has different methods.
Object-oriented: Encapsulates functions that implement a class of functions, each with a different object.
Introduction to Object-oriented technology
- class: used to describe a collection of objects that have 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.
- Class variables: class variables are common throughout the 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.
- data members: class variables or instance variables are used to manipulate the data related to the class and its instance objects.
- 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.
- instance variable: A variable defined in a method that acts only on the class of the current instance.
- inheritance: 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 simulation "is a (is-a)" relationship (for example, Dog is a animal). Also known as "parent class" and "subclass"
- instantiation: Creates an instance of a class, the concrete object of the class.
- method: a function defined in a class.
- 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.
First, classes and objects
Object-oriented programming is a way of programming, in which the two most important concepts are classes and objects. Object-oriented programming is the use of "classes" and "objects".
What a class is: A class can be seen as a "template" for something we do, and a template can contain multiple functions that implement specific functions.
From a programming standpoint, a class is a program group unit of Python, like a function and a module, with three important features that make it more useful to create new objects.
1. Multiple instances
A class can be seen as an object's production shop, and each time a class is called, a new object is generated for a separate namespace. Each object produced by a class can read the properties of the class and get its own namespace to store the data, which is different for each object.
2. Customizing through inheritance
We can extend this class by redefining its properties outside of the class.
3. Operator overloading
By providing a specific protocol method, a class can define an object to operate on the built-in type accordingly.
Object: An object is an instance created from a template, and after instantiating a class we can use an instance object to execute a function of the class to accomplish a specific task.
· By object Execution method: Object name. Method Name ()
#!/use/bin/env python#_*_ coding:utf_8 _*_#Defining ClassesclassFU:#Note No parentheses #to create a function in a class, a function in a class is called a method defWelcome (self):Print('Create a first class') defHello (self,name):#Self is a formal parameter and must be filled in. Print('hello,%s'%name)#Defining Objectsobj =FU () obj.welcome ()#Execute the Welcome methodObj.hello ('Sir Zhang')#Execute the Hello () methodView CodeTwo, object-oriented three main features object-oriented Programming: Object creation, function programming through object execution methods: There are three main features of an object-oriented execution function:encapsulation, inheritance, polymorphism(The interview must be a wooden one)(1) packageEncapsulation is a two-step process: encapsulating and calling the Step1 package encapsulates a method that implements a class of functionality.
classF1:def __init__(self,name,age):#constructs methods that are automatically executed according to the objects created by the classSelf. Name =name self. Age= Age#to create an object from a class obj#automatic execution of __init__ methodsObj1 = F1 ('Zhang',' -')#encapsulating the zhang,20 to the Obj1Obj2 = F1 ('Wang',' -')#encapsulating the wang,30 to the Obj1View CodeSelf is a formal parameter when executingObj1=F1(' Zhang ', ' 20 ' ) obj2 = F1 ( ' Wang ' , ' "< Span data-wiz-span= "Data-wiz-span") Step2 Call1. Call the encapsulated content directly from the object. Object. Property name
classF1:def __init__(self,name,age):#constructs methods that are automatically executed according to the objects created by the classSelf. Name =name self. Age= Age#to create an object from a class obj#automatic execution of __init__ methodsObj1 = F1 ('Zhang',' -')#encapsulating the zhang,20 to the Obj1Obj2 = F1 ('Wang',' -')#encapsulating the wang,30 to the Obj1View Code
2. Indirectly invoking the encapsulated content via self
classF1:def __init__(self,name,age):#constructs methods that are automatically executed according to the objects created by the classSelf. Name =name self. Age= Agedefinfo (self):Print(self.) Name)Print(self.) Age)#to create an object from a class obj#automatic execution of __init__ methodsObj1 = F1 ('Zhang',' -')#encapsulating the zhang,20 to the Obj1Obj1.info ()#python By default passes obj1 to self, which is obj1.info (obj1), self. Name = ' Zhang '; Age = ' A 'Obj2 = F1 ('Wang',' -')#encapsulating the wang,30 to the Obj1Obj2.info ()View CodeExample of a moral integrity drop
classfoo:def __init__(self,name,age): Self.name=name Self.age= Agedefoption1 (self):Print('%s,%s, go up the hill to chop Wood'%(self.name,self.age))defOption2 (self):Print('%s,%s, drive to the northeast.'%(self.name,self.age))defOption3 (self):Print('%s,%s,xxoo'%(self.name,self.age)) Obj2= Foo ('Xiao Ming','Ten') Obj2.option1 () Obj2.option3 () obj1= Foo ('Lao Wang',' +') Obj1.option2 ()>>>Xiaoming,10, go up the hill to chop Xiao Ming,10, Xxoo Lao Wang,40, drive to Tohoku.View Code
(2) Inheritance for Python syntax, classes and objects can be simplified to an expression: Object.attribute, when a class is encountered, the following actions are performed:, Find where attribute first appears, search for object first, and then search for all classes above the object, from bottom to top, from left to right. Subclasses inherit the properties of the parent class, overriding the properties defined by the parent class when the child class re-defines the variable name of the parent class. for object-oriented inheritance, the fact is that multiple classes of common methods are extracted into the parent class, and the subclass inherits only the parent class without having to implement each method in one.
class C1: def func (): Pass class C2 (C1) # Subclass inherits the parent class, with all the methods of the parent class def obj ( )Pass = C2 () # To create a child class object Zi.func () # executes a method inherited from a parent class
View CodeMultiple InheritanceIn the example above we inherited only one method of the parent class, but for Python, its classes can inherit multiple classes, and two Java and C # can inherit only one class. So the question is, when Python inherits more than one class, which of the same methods in the class is used? To solve this problem, Python uses two different ways, namelyDepth FirstAndBreadth First
Python Object-oriented