python--day7--advanced Object-oriented programming

Source: Internet
Author: User

Introduced

Learning record ....

Directory:

    1. Object-oriented Advanced Syntax section

    • Field

    • Method

    • Property

Special member reflection of the classical class of modern class class

Class

    • Field


      • Normal field

      • Static fields

    • Method


      • Common methods

      • Class method

      • Static methods

    • Property


      • Normal properties

      • Private properties

Field

Ordinary fields belong to the object, instance variables, exist in each instance, when the instance, open an instance object,

Static fields belong to the class, class variables, in memory exist in the class, no instantiation, also exist.

Method

class animal:    def __init__ (Self,name):         self.name=name    hobbie =  ' Meat '     @ classmethod # #类方法, cannot access instance variable     def talk (self):         print ("{}  is   talking". Format (Self.hobbie))       @staticmethod   # #静态方法, cannot access class variables   and instance variables, most of the cases do not, the class's Toolbox     def walk ():         print (" is   talking")       @property    # #属性, the following is not a method, it becomes a property, but you can still access instances and class variables     def   Habit (self):         print ("{}  habit  is   xxoo ". Format (Self.name)) d = animal (" Hequan ") Animal.talk () D.talk () D.walk () d.habitmeat   is   talkingmeat  is   talking is   talkinghequan  habit  is   xxoo


Private Properties __

Self.__num=num

Classic class: Limited depth

New class: Breadth First

Special members of the class

    1. __doc__ Printing Notes

    2. __MODULE__ represents the object of the current operation in that module

    3. __CLASS__ represents the class of the object that is currently being manipulated

    4. __init__ constructs a method that automatically triggers execution when an object is created through a class

    5. The __del__ destructor automatically triggers execution when the object is freed in memory. After the execution is complete, it is destroyed uniformly.

    6. The __call__ object is appended with parentheses, triggering execution

    7. __DICT__ View all members in a class or object,

    8. __new__ \ __metaclass__ the class's build call order is __new__---__call__--and __init__

Reflection

Mapping or modifying the state, properties, methods of a program while it is running through a string

    1. Hasattr

    2. GetAttr

    3. SetAttr

    4. Delattr

If __name__== "__main__":

Server = WebServer (' localhost ', 3306)

If Hasattr (Server,sys.argv[1]):

Func = GetAttr (server,sys.argv[1]) # #获取执行后面加的值 like start

Func ()


This article is from the "what-all" blog, please be sure to keep this source http://hequan.blog.51cto.com/5701886/1886936

python--day7--advanced Object-oriented programming

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.