Python--Object-oriented first knowledge

Source: Internet
Author: User

1, object-oriented first knowledge

Process-oriented program design is the core of process (pipeline thinking), the process is to solve the problem of the steps, process-oriented design is like a well-designed pipeline, consider when to deal with what things.

The advantage is that it greatly reduces the complexity of the writing process, and only needs to stack the code along the steps to be performed.

The disadvantage is: a set of pipeline or process is to solve a problem, code reaching.

Application scenario: Once you have completed a very rarely changed scenario, the notable examples are Linux kernel, git, and Apache HTTP server.

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 world exists in all things are objects, not exist can also be created. Object-oriented programming good for example, to design a journey to the east, the Tathagata to solve the problem is to pass the scriptures to the Eastern Earth Datang, the Tathagata thought to solve this problem requires four people: Tang's monk, Sha Monk, Pig Eight commandments, Monkey King, everyone has their own characteristics and skills (this is the concept of objects, Characteristics and skills corresponding to the object's properties and methods, but this is not fun, so the Tathagata arranged a group of ghosts and goblins, in order to prevent the four people in the study on the road was killed, and arranged a group of immortal escort, these are objects. Then the beginning of the lessons, master and master Four with ghosts and demons to each other until the end of the canon. The Tathagata does not control the four people follow what process to take.

Object-oriented program design.

The advantage is that 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. So we often see a game of people changes in a certain parameter is likely to lead to the ability of the bully to appear, a knife to kill 3 people, the game is out of balance.

Application scenario: frequently changing requirements of the software, the general needs of the changes are concentrated in the user layer, Internet applications, enterprise internal software, games, etc. are object-oriented programming is a good place to play.

Object-oriented programming in Python is not all.

Object-oriented programming makes it easier to maintain and extend programs, and can greatly improve program development efficiency, and the object-oriented program makes it easier for people to understand your code logic, making team development easier.

Learn about nouns: classes, objects, instances, instantiation

class : A category of things (people, dogs, tigers) with the same characteristics

Object/Instance : A specific thing (next door Jen, Downstairs wong Choy)

instantiation : The class-to-object process (which is not apparent in life, we explain it later)

Object-oriented features:

1, the function encapsulates a function, and object-oriented encapsulates several related functions.

2, object-oriented abstraction, it is an idea that stands in the perspective of God to understand it.

3, the program can be extended, the objects are independent. Coupling, difference.

There are three stages within the instantiation process:

1, it opens up an object space in memory.

2, automatically executes the __init__ method in the class, and automatically passes the object space to the self parameter, and the other parameters are passed in manually.

3, execute the __init__ method, encapsulating the corresponding property for the object space.

2, class-related knowledge declaration
def func (ARS):     " function Body " func ()
declaring functions
class class Name:     " class Body "    # we create a class class Data:     Pass
declaration of a class
class Person:  #  Define a class    "man"  #  People's role attributes are people     def Walk (self):  #  People can walk, that is, there is a way to walk, also known as dynamic properties        Print ("Peerson is walking ... ")

Print (Person.role) # View People's Felo properties
Print (Person.walk) # refers to the way people walk, note that this is not the 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)

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.

Supplement to 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__#the name of the class (string)The class name.__doc__#the document string for the classThe class name.__base__#the first parent class of a class (speaking of inheritance)The class name.__bases__#a tuple of all the parent classes of the class (speaking of inheritance)The class name.__dict__#Dictionary properties of the classThe class name.__module__#module where the class definition residesThe class name.__class__#class for instance (in modern class only)
Related knowledge of the object
classclass Name:def __init__(self, parameter 1, parameter 2): Self. Property of Object 1=parameter 1 self. Properties of the Object 2=Parameter 2defMethod Name (self):Pass    defMethod Name 2 (self):PassObject Name= Class Name ($)#An object is an instance that represents a specific thing.                  #class name (): Class name + parenthesis is the instantiation of a class, equivalent to calling the __init__ method                  #Arguments in parentheses, parameters do not need to pass self, others correspond to parameter one by one in Init                  #The result returns an objectObject name. object's Properties 1#View the properties of an object, directly using the object name. Property nameThe name of the object. Method Name ()#call a method in the class, directly with the object name. Method Name ()

1, Object View all properties of object space with __dict__
2, the object can be the object of the property of the increase and deletion, method with omnipotent. Point
3, objects can only be checked for properties of class space
Exercise A: The terminal output The following information Xiao Ming, 10-year-old, male, go up the hill to firewood Xiao Ming, 10-year-old, male, driving to the northeast Xiao Ming, 10 years old, male, the most love big health old Lee, 90 years old, male, go to the mountains to cut wood Old Lee, 90 years old, male,
ExercisesClass namespace and Object namespace

Creating a class creates a namespace for a class that stores all the names defined in the class, called Properties of the class

The class has two properties: static property and dynamic property

    • A static property is a variable that is defined directly in the class
    • A dynamic property is a method defined in a class

Where the data properties of a class are shared to all objects

>>>ID (egg.role)4341594072>>>ID (person.role)4341594072

The dynamic properties of a class are bound to all objects.

>>>egg.attack<bound method Person.attack of <__main__. Person object at 0x101285860>>>>>person.attack

Creating an object/instance creates an object/instance namespace that holds the name of the object/instance, called the object/instance property

In the Obj.name will first find the name in Obj's own namespace, can not find the class to find, the class can not find the parent class ... Throw an exception if you can't find it at the end

classGamerole:rule='Game Rules'HP= 1800def __init__(SELF,AREA,NICKNAME,HP,AD): Self.area=Area Self.nickname=Nickname SELF.HP=HP Self.ad=ADdefAttack (self):Print('who has cast an attack')    defpen (self): Self.pen=10000Gailun= Gamerole ('Demassey!','Bush LUN', 1000,75) Yasuo= Gamerole ('Ayonia','Nursery', 500,150) Penzi= Gamerole ('China','Keyboard Man', 10,100)#Penzi.pen = 10000Penzi.pen ()Print(Penzi.__dict__)#print (GAILUN.HP)#gailun.attack = 666 # Adds an attribute to an object attack = 666#print (gailun.attack)Gailun.rule= Gailun.rule#' Rules of the game ' * * * * More difficult to understand#gailun.rule = game Rules '#object. Property name = ' Game Rule 'Print(Gailun.rule)
Thinking Comprehension

1, the object calls the property in the class and method is to find from their own space, without this property he will be from the class object pointer from the class to find, not found in the class, will be found from the parent class. 2, the object can only view the properties in the class, and cannot delete or remove attributes from the class. 3, objects that are instantiated from the same class are not accessible to each other, and objects with non-homogeneous instantiation may have access to each other. 4, the object encapsulation property with __init__, can be in any position.

Python--Object-oriented first knowledge

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.