Python----Object-oriented first knowledge

Source: Internet
Author: User

1. Object-Oriented Programming

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.

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)

Structure:

class A:    #A is a class    'Alex'    # static property, static variable    def dunc1 (self):    # function, dynamic property, method        Pass


Class Shopping_car:
def __init__ (self): #特殊方法
Pass
def register (self):
Pass
def login (self):
Pass
def shoppingcar (self):
Pass

A class is a kind of thing that has the same properties or functions, and a class can encapsulate multiple functions, that is, multiple methods, and classes are easy to extend.

Now we build a human, common attribute, such as ' Advanced animals ', and then act, that is, the way to eat, to work:

 class   Person:animal  = "   advanced animal   " def   Eat (self):  print
     ( "  Want to eat?   " )  def   work ( Self):  print  ( '   that works  " ) 

# 1, view all properties and methods in the class __dict__
# Print (person.__dict__)
# Print (person.__dict__[' animal ') # through the __dict__ method of the individual properties and methods can be checked, but cannot be deleted or modified
# person.__dict__[' Animal ' = ' low animal '
# person.__dict__[' name '] = ' Alex '
# Print (person.__dict__)
# in work, learning is generally used to __dict__ view all the properties and methods in a class without doing anything else.
#2, view, (Add and remove) A class, some attributes with a universal point.
# print (person.animal) # Check
# Print (person.language)
# person.name = ' Alex ' # increase
# Print (person.name)
# person.animal = ' Lower animal ' # change
# del Person.walk_way # Delete
# Print (person.__dict__)
#3, the operation method is generally not through the class name operation!!!
# person.__dict__[' work ' (111) not recommended by __dict__ execution method
# person.work (666)

2. What is instantiation

There is a special method in the class __init__ (self) function, what is the use of this function, see the following code:

class Person :         ' Advanced Animals '        def __init__ (self):             Print ('111')         def Eat (self):             Print ('222') person () #111

It is found that when you execute the class, __init__ (self) executes directly, because the person () is an instantiated process that automatically executes the code in the INIT function to instantiate the object, and usually we write the following pattern:

class Person :     ' Advanced Animals '    def __init__ (self):         Print ('111')     def Eat (self):         Print ('222'= person ()

Then you will find that obj. You can use all the methods of the person class and view the properties, then why do you want to execute the INIT function first, in fact, the INIT function is to add unique properties to the object, the above example we can see in the __dict__ method obj there is nothing

 class   Person:animal  = "   advanced animal   " def  __init__   (self): 
    print  ( " 111   " )  def   Eat ( Self):  print  ( '  222  "  ) obj  = person () #111  print  (Obj._ _dict__ ) #{} 

You can see that {} is an empty dictionary. In fact, the process of creating an object instantiation is: 1. First, the memory generates an object space, and then this space points to the INIT function of the class, which is understood to be the function self parameter, which means that the object space address of obj is directed to the self to Init, so that the object can execute the INIT function. There is no pass property in the Init function except for the default self address parameter, and here is another example.

Instantiation is an object that is generated by assigning a class to a specific property and executing the method behavior, and the syntax is obj = Class ()

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=10000

The above defines a Gamrole class with the attributes of rule and HP, followed by the __init__ function and the attack with the pen function.

Then our husband is a character called Galen, generating objects:

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)
That way I passed the three extra parameters to init except Seft, so that when I called the __dict__ method, I could see that the instantiated Guilun object had its own properties:
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)Print(Gailun.__dict__#{' area ': ' Demassey yes ', ' nickname ': ' Bush lun ', ' hp ': +, ' ad ': 75}
This allows us to instantiate an object called Guilun.
Schematic instantiation process:








Summarize:

# 1 Why can an object invoke a property and method in a class and just call it and not change it?
# Gailun. Attribute names are first searched from their own space, without this attribute he will look through the class object pointer from the class, not found in the class, will be found from the parent class.
# Print (GAILUN.HP)
# gailun.attack = 666 # Adds an attribute to an object attack = 666
# Print (Gailun.attack)
Gailun.rule = gailun.rule # ' game rules '
# gailun.rule = Game Rules '
# object. Property name = ' Game Rule '
Print (Gailun.rule)
Gailun.nikename = ' Galen '
Print (Gailun.nickname)
# objects can only view properties in a class, and cannot delete or remove attributes from a class.

# 2, can a class invoke the properties of an object? No


# Print (Gamerole.area)
# 3, can not call each other between objects and objects?
# objects that are instantiated from a class cannot be accessed from one another.
# objects that are instantiated by different classes may have access to each other.

# Encapsulates properties for an object: __init__ anywhere.



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.