Python OO and PythonOO

Source: Internet
Author: User

Python OO and PythonOO

I want to learn Java at school in my sophomore year,

The most awesome OO thought was finished on three pages, or from Tsinghua University Press.

Later, I learned what OO is based on my own video and code.

 

Now, you can learn Python in your own way:

OO is basically encapsulation, inheritance, and polymorphism.

 

The first is inheritance:

Define a class:

Python code
  1. Class Bird (object ):
  2. Have_feather = True
  3. Way_of_reproduction = 'egg'

 

Call this class:

Python code
  1. Summer = Bird ()
  2. Print summer. way_of_reproduction

Unlike Java, Python does not need new to instantiate classes.

 

Similarly, Python classes can be set as follows:

Python code
  1. Class Bird (object ):
  2. Have_feather = True
  3. Way_of_reproduction = 'egg'
  4. Def say (self, word = 'hihi '):
  5. Print 'I say:' + word

 

Note that all class functions must have at least one parameter, which must be self.

Functions other than classes do not have this restriction.

Python code
  1. Chk = Chicken ()
  2. Print chk. have_feather
  3. Print chk. sat ('hello ')

 

_ Init _ () method

_ Init _ () is a special method (special method ). Python has some special methods. Python will process them in a special way. The name of a special method is characterized by two underscores.

The special feature of the _ init _ () method is that if you define this method in the class, once you create an object based on this class, python will automatically call this method (this process is also called initialization ).

For example:

Python code
  1. Class happyBird (Bird ):
  2. Def _ init _ (self, more_words ):
  3. Print 'we are happy birds. ', more_words
  4. Hb = happyBird ('Happy, Happy! ')

 

Overload of the parent method:

Python code
  1. Class Hello (object ):
  2. Name = 'hello'
  3. Def _ init _ (self ):
  4. Self. name = 'My name is hello'
  5. # The parameters in the class must contain the self Parameter
  6. Def sayhi (self ):
  7. Print 'Hi you'
  8. Class World (Hello ):
  9. Def _ init _ (self ):
  10. # The variable name initialized by the parent class is accessed here.
  11. Print 'before: ', Hello. name
  12. Super (World, self). _ init __()
  13. # Because the initialization constructor of the parent class is called, the variable changes of the parent class are inherited.
  14. Print 'after: ', self. name
  15. # Similar to method overload
  16. Def sayhi (self, word = 'baby '):
  17. # Call the sayhi method of the parent class
  18. Super (World, self). sayhi ()
  19. Print 'Hi' + word
  20. Def sayWorld (self ):
  21. Print 'Hi, hello world'
  22. If _ name _ = '_ main __':
  23. C = World ()
  24. C. sayhi ()
  25. C. sayWorld ()

 

In addition, python allows multi-inheritance, but this is a very dangerous operation. We recommend that you do not use it at will.

 

For Python polymorphism, just like JavaScript, you can directly access object attributes without using interfaces or type conversion.

For the type judgment, there are the type () function, andIsinstance ()Determines whether a function is a subclass.

Python code
  1. Isinstance (object, classinfo)
  2. Determines whether the instance is a class or an object is a variable.
  3. Classinfo is a type (tuple, dict, int, float)
  4. Determines whether the variable is of this type.
  5. Class objA:
  6. Pass
  7. A = objA ()
  8. B = 'A', 'V'
  9. C = 'a string'
  10. Print isinstance (A, objA)
  11. Print isinstance (B, tuple)
  12. Print isinstance (C, basestring)
  13. Output result:
  14. True
  15. True
  16. True

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.