Process-oriented and object-oriented introduction 1. Process oriented:
Definition: Process-oriented program design is the core of process (pipeline thinking), the process is the steps to solve the problem, process-oriented design is like a well-designed pipeline, consider when to deal with what things.
Advantage: Greatly reduce the complexity of the writing program, just follow the steps to be executed, stacking the code can be.
Disadvantage: A set of pipeline or process is used to solve a problem, code reaching.
2. Object-oriented:
Definition: Object-oriented program design is the core of the object (God-like thinking), to understand what the object is, we must regard themselves as God, God in the eyes of the existence of all things are objects, not exist can also be created.
Advantages: It solves the extensibility of the program. A single modification of an object is immediately reflected in the entire system, such as the character of a character parameter in the game and the ability to modify it easily.
Cons: Poor controllability, inability to process-oriented programming pipelining can accurately predict the problem of the processing process and results, the object-oriented program once started by the interaction between the object to solve the problem, even God can not predict the end result.
Ii. Introduction to Object-oriented family members 1. Primary knowledge classes and objects
Object:
(1) Means all objects (' Everything is the object ')
(2) Different objects have different properties, and everything changes--the method of the object.
(3) There are 3 aspects to any one property: the object to which the property belongs, the value of the property, and the name of the property. Some properties of an object are not one-layer-invariant.
Class:
(1) Birds of a feather--class definition Object-biscuit mold Production Biscuit
(2) A class is made up of the same characteristics, and a class is a blueprint for defining variables and methods for all objects of the same class.
(3) The generality of an object is abstracted as a class, and the instantiation of a class is an object.
1. Declaration of class:
def functionname (args): ' function document String '
' Class class name: ' Class ' document String ' body ' #我们创建一个类class Data: Pass
Class Person: #定义一个人类 role = "Person" #人的角色属性都是人 def Walk (self): #人都可以走路, that is, a walking method, also known as a dynamic property print ("Person is walking ...")
2. Classes have two functions:
Property Reference:
Class Person: #定义一个人类 role = "Person" #人的角色属性都是人 def Walk (self): #人都可以走路, that is, there is a way to walk Print ("Person is walking ...") print (Person.role) #查看人的role属性print (person.walk) #引用人的走路方法, note that this is not a call
Instantiation: The class name parentheses are instantiated, automatically triggering the run of the __INIT__ function, which can be used to customize each instance of its own characteristics
Class Person: #定义一个人类 role = ' person ' #人的角色属性都是人 def __init__ (self,name): self.name = name # Each character has its own nickname; def walk (self): #人都可以走路, that is, there is a walk method print ("Person is walking ...") print (Person.role) #查看人的role属性print ( Person.walk) #引用人的走路方法, note that this is not a call
The process of instantiating is a class-to-object procedure
Originally we had only one person class, in this process, produced an egg object, has its own specific name, attack and health value.
Syntax: Object name = class Name (parameter)
Class Person: #定义一个人类 role = ' person ' #人的角色属性都是人 def __init__ (self,name): self.name = name # Each character has its own nickname; def walk (self): #人都可以走路, that is, there is a way to walk print ("Person is walking ...") Aray = person (' ZBK ') print (aray.name) #查看属性直接 object name. Property name Print (Aray.walk ()) #调用方法, object name. Method Name ()
About self
Self: Automatically passes the object/instance itself to the first parameter of __init__ when instantiated, and you can give him an individual name, but the normal person will not do so.
Because you don't know anyone because you're blind.
The addition of class attributes:
A: Where do the properties of the class we define are stored? There are two ways to view Dir (class name): A Name list class name is detected. __dict__: A dictionary is found, key is the property name, value is the attribute two: Special Class attribute class name. __name__# class Name (String) class name. __doc__# Class name of the document String class. The first parent class of the __base__# class (Speaking of inheritance) class name. A tuple of all the parent classes of the __bases__# class (Speaking of inheritance) class name. The class name of the dictionary attribute of the __dict__# class. The __module__# class defines the module class name. Class for __class__# instances (in modern class only)
Object-oriented summary--fixed mode for definition and invocation
Class Name: def __init__ (self, parameter 1, parameter 2): Self . Property of the Object 1 = parameter 1 self . property of Object 2 = parameter 2 def method name (self):p Method Name 2 (self):p the name of the #对象就是实例, which represents a specific thing #类名 (): Class name + parenthesis is the instantiation of a class, equivalent to invoking the __init__ method #括号里传参数, The parameter does not need to pass self, and the other is #结果返回一个对象对象名 corresponding to the formal parameter one by one in Init . The object's property 1 #查看对象的属性, directly with the object name. The name of the object. method Name () #调用类中的方法, directly with the object name. Method Name () can be
Python = = "Object Oriented"