The OO idea of Python

Source: Internet
Author: User

When I was a sophomore, I studied Java at school,

The most awesome oo idea, with 3 pages of paper is finished, or Tsinghua University Press.

Later all by themselves Chew video, chew code to understand what is called OO.

Learn Python now and learn it in your own way:

The basic of OO is encapsulation, inheritance, 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 require new to instantiate a class.

Similarly, the Python class below is a way to fix the method:

Python code
    1. Class Bird (object):
    2. Have_feather = True
    3. way_of_reproduction = ' egg '
    4. def say (self, word=' Hi Hi '):
    5. print ' I say: ' + word

Note that functions of all classes must have at least one argument, and this argument must be self.

A function other than a class does 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 approach (special method). Python will have some special methods, and Python will handle them in a special way. The name of the special method is characterized by a two underscore before and after.

The special of the __init__ () method is that if you define this method in a class, Python automatically calls this method (which is also called initialization) once you have created the object from that class.

Such as:

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! ')

Overloads of the Parent class method:

Python code
  1. Class Hello (object):
  2. name = ' Hello '
  3. def __init__ (self):
  4. self.name=' My name is Hello '
  5. #类中的参数必须带有self参数
  6. def sayhi (self):
  7. print ' Hi you '
  8. Class World (Hello):
  9. def __init__ (self):
  10. #这里访问的是父类初始化的变量名
  11. print ' before: ', Hello.name
  12. Super (World, Self). __init__ ()
  13. #由于调用了父类的初始化构造函数, inherits the change of the parent class's variable
  14. print ' after: ',self.name
  15. #近似于方法重载
  16. def sayhi (self,word=' baby '):
  17. #调用父类sayhi方法
  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 is allowed to inherit multiple, but this is a very dangerous operation, it is recommended not to use it casually.

With regard to Python polymorphism, like JavaScript, direct access to the properties of an object without the need for an interface, no type conversion.

For the type of judgment, there are the type () function of the grab, and the isinstance () function determines whether the subclass of a function.

Python code
  1. Isinstance (object, ClassInfo)
  2. Determine if the instance is this class or object is a variable
  3. ClassInfo is a type (tuple,dict,int,float)
  4. Determine if 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

The OO idea of Python

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.