Python Full Stack Development * 18 Object-oriented Knowledge points summary * 180530

Source: Internet
Author: User


18 Object-oriented first knowledge 1
Class Person:
Level= "Advanced Animals"
Mind= "Have thought"
def __init__ (Self,name,age,gent,area,hobby):
Self.name=name
Self.age=age
Self.gent=gent
Self.area=area
Self.hobby=hobby
Print ("__init__")
Pass
def walk (self):
Pass
Def eat (self):
Pass
Aq=person ("Q", "16", "Male", "China", "bragging")
Print (aq.__dict__)
I. Class
1. A class of things with the same attributes and skills.
2. Structure of the class (two parts)
(1). Static variable (field)
Level= "Advanced Animals"
Mind= "Have thought"
(2). Dynamic Methods (functions)
def __init__ (Self,name,age,gent,area,hobby):
Self.name=name
Self.age=age
Self.gent=gent
Self.area=area
Self.hobby=hobby
Print ("__init__")
Pass
def walk (self):
Pass
Def eat (self):
Pass

3: Class name call variable, method
(1). Class name View all variables (fields) in a class, methods (functions)
The print (class name. __dict__) #类中所有的东西 attribute method can only be used to find and delete items in the class.
(2). Class name lookup static variable (field)
Common: By class name. Variable name
Check: print (Person.mind)
Add: person.create= "creative"
Change: person.mind= "The Walking Dead"
Delete: Del person.mind
Infrequently used: Print (class name. __dict__[mind])
(3). The operation of the class name on the method (function)
Common: Class name. Method Name (function)
Person.walk (parametric)
Infrequently used: class name. __dict__["Walk"]

Two. Object.
1. The object is the concrete manifestation of the class.
2. Instantiating an Object
Syntax: Class name () is equivalent to instantiating an object
3 Execution Process
(1). Generate an object space; (aq= class name () AQ is an object space)
(2). Automatically executes the __init__ method in the class and passes the object space to self; (Def __init__ (self) passes aq to self)
(3). Execute the __init__ method to add attributes to the object and return the perfect object to the class name.
def __init__ (Self,name,age,gent,area,hobby):
Self.name=name #self. Name In name indicates the name reference to the right of the object property equals sign
Self.age=age
Self.gent=gent
Self.area=area
Self.hobby=hobby
Print ("__init__")
4. Object lookup All properties in an object
Print (aq.__dict__)
5. Static variables in object manipulation objects
Check: object. Name # print (aq.name)
Increment: Object. job= "Student" # aq.job= "Stu"
Delete: Del object. Age
Change: Object. gent= "Male"
6. The static variables in the object operation class can only be queried, cannot be deleted or modified.
The object name. Variable (level) in class # print (Aq.level)

7. Methods in the Object execution class
The object name. Walk () # Aq.walk () # automatically receives object space without having to pass arguments to the self position.

Add: __init__ is called construction mode, you can also append properties when executing a method (temporary)
Print ("__init__")
Self.eye= "Eye"
You can also add properties to an object in the non-__init__ of the class, if you must first perform this method
def walk (self):
Print ("Go on foot")
Self.skincolor= "Skincolor".

Python Full Stack Development * 18 Object-oriented Knowledge points summary * 180530

Related Article

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.