Python quick learning 05: Object-oriented

Source: Internet
Author: User

Python objects are the core part of the Python language. Python uses classes and objects to program object-oriented (OOP. The main purpose of object-oriented is to improve the reuse of programs. Python class example class Bird (object): have_feather = True way_of_reproduction = 'egg' print (id (Bird) # built-in function id (): unique ID # two variables (attributes): have_feather, and way_of_reproduction. instantiate summer = Bird () print (summer. way_of_reproduction) # object. attribute (object. attribute) Some "behavior" attributes of the method class in the access property value class are method ). In Python, you can define functions in the class to describe the method. Sample copy code class Bird (object): have_feather = True way_of_reproduction = 'egg' def move (self, dx, dy ): position = [0, 0] position [0] = position [0] + dx position [1] = position [1] + dy return position summer = Bird () print ('after move: ', summer. move (5, 8) Copy Code # The parameter has a self, which is used to facilitate reference of the object itself. The first parameter of the method must be self. The subclass category itself can be further subdivided into child classes. In OOP, we use inheritance to express the above concepts and copy the code class Chicken (Bird ): way_of_move = 'walk 'possible_in_KFC = True class Oriole (Bird): way_of_move = 'fly' possible_in_KFC = False summer = Chicken () print (summer. have_feather) print (summer. move (5, 8) copy the Code # Bird in the brackets when defining the class. This indicates that Chicken is a child of Bird, that is, Chicken inherits from Bird. Naturally, Bird is the parent class of Chicken. Chicken will have all the attributes of Bird. # Although I only declared that summer is a chicken class, it inherits the attributes of the parent class (whether it is the variable property have_feather or the method property move). The self example copies the code class Human (object): laugh = 'hahahahaha' def show_laugh (self): print (self. laugh) def laugh_100th (self): for I in range (100): self. show_laugh () li_lei = Human () li_lei.laugh_100th () Copy Code # Here there is a class attribute laugh. In the show_laugh () method, the value of this attribute is called through self. laugh. # Other methods can be called in the same way. The show_laugh () method is called in method laugh_100th. _ Init _ () is a special method (special method) used for initialization. Python has some special methods. Python will treat it specially. EXAMPLE class happyBird (Bird): def _ init _ (self, more_words): print ('We are happy birds. ', more_words) summer = happyBird ('Happy, Happy !! ') There will be the following output We are happy birds. Happy, Happy! # The summer object is created, but the _ init _ () method is automatically called. Summary # All Python objects have three features: identity, type, and value.

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.