Day7----Object-oriented programming advanced

Source: Internet
Author: User

The content of this section:

    • Object-oriented Advanced Syntax section
      • Static methods, class methods, property methods
      • Special methods for classes
      • Reflection
    • Exception handling
    • Socket Development Basics

Static methods

Its only association with a class is the need to call this method through the class name

#静态方法实际跟类没关系, there is no self variable.
#只是名义上贵归类管理, in fact, in a static method, you cannot access any property in a class or instance

1. When invoked, the instance itself is passed to the Eat method, i.e. (d)

2. Remove the self parameter in the Eat method, but this also means that you cannot pass self in eat. Calls to other variables in the instance

#!/usr/bin/env python#-*-coding:utf-8-*-#AUTHOR:DCCclassDog (object):def __init__(self,name): (Self,food):Print("%s is eatting%s"%(,food)) d= Dog ("Dog1") ("steamed Bun")#Static MethodsclassCat (object):def __init__(self,name): @staticmethod#static methods do not really matter with classes, there is no self variable.     #just nominal expensive collation management, in fact, in a static method, cannot access any property in the class or instance    defEat (Self,food):Print("%s is eatting%s"%(Self,food)) d= Cat ("Dog1") ("CAT1","Baozi")

Day7----Object-oriented programming advanced

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: 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.