Python notes: class and inheritance, python notes inheritance

Source: Internet
Author: User

Python notes: class and inheritance, python notes inheritance

  1. #-*-Coding = UTF-8 -*-
  2. # The file encoding syntax is: coding [: =]/s * ([-/w.] +)
  3. # If no encoding is specified, the default value is ASCII.
  4. # Note that the encoding of physical files must also conform to the encoding specified by the language.
  5. # More can refer to: http://python.org/dev/peps/pep-0263/
  6. Class CBase:
  7. ''' Resource-related class''' # This description can be output through CBase. _ doc _ or class instance. _ doc _
  8. Counter = 0; # This is a class variable, equivalent to a static variable in c ++, public
  9. _ Counter = 1; # The variable is considered a private variable with double underscores (_ counter = 1 ).
  10. # Cannot be accessed outside the class; otherwise, it is considered a public variable.
  11. # In Python, _ init _ is equivalent to the constructor of C ++,
  12. # Define the object variable in _ init _, which is equivalent to a common variable in c ++.
  13. Def _ init _ (self, name ):
  14. Self. name = name # common object variable, public
  15. Self. _ private1 = 2 # private common variable, private
  16. Print ('_ init _ base ')
  17. # _ Del _ is equivalent to the destructor of c ++
  18. Def _ del _ (self ):
  19. Print ('base destruct ')
  20. # Any class method must have at least one parameter
  21. # This parameter is equivalent to the this pointer of C ++.
  22. # This parameter must be the first parameter
  23. # Conventions generally write self
  24. Def Help (self ):
  25. '''''''Help doc''' # doc of the method, reference syntax [class name | instance]. method name. _ doc __
  26. Print ('--- base help begin ---/N', self. name) # reference common variables here
  27. Print (CBase. _ counter) # Even if the methods of this class reference their own static variables,
  28. # You also need to specify the class name or self.
  29. Print (self. _ private1) # private variables can only be referenced by this class of methods.
  30. Print ('--- base help end ---/N ')
  31. Def Test (self ):
  32. Print ('base test ')
  33. # Class inheritance
  34. # Multi-inheritance is supported. The syntax class CC (CA, CB)
  35. Class CHigh (CBase ):
  36. Def _ init _ (self ):
  37. CBase. _ init _ (self, 'high') # manually input the first parameter when calling the construction of the base class
  38. Print ('_ init _ high ')
  39. Def _ del _ (self ):
  40. # CBase. _ del _ (self), The destructor of the base class cannot be called.
  41. # The destructor of the base class will not be automatically called.
  42. Print ('high destruct ')
  43. # The method of the inherited class overwrites the method of the same name of the base class.
  44. Def Test (self ):
  45. Print ('high test ')
  46. # Python does not have the concept of method overloading.
  47. # The Last defined method in the source file will overwrite the previous method with the same name.
  48. # The second parameter name must be passed when Test is being called.
  49. # The preceding Test method with a parameter is overwritten.
  50. Def Test (self, name ):
  51. Print ('high Test have arg: ', name)
  52. Rh = CHigh ()
  53. # Rh = CBase ('hello ')
  54. Rh. Help ()
  55. Rh. Test ();
  56. Print (CBase. counter)
  57. Print (rh. name)

Who can use the popular method to inherit the python class? Tell the younger brother about the inheritance of the class.

A general example is:
There is already a class called bird
Its flying methods/functions

Then you implement another sparrow class.
If no class is inherited
Then you need:
First implement the Flying Method
Then other methods (and attributes) specific to Sparrow are implemented separately)

This method is similar to the method of flying. It is obvious that it is a common method used by all species of birds.
Therefore, to avoid this, every other bird and every other bird must implement this method separately.
Therefore, we get a base class, that is, the basic class, the main class.
It implements some common things that everyone shares.
Including many methods and many attributes

Then the other child classes
After this base class is integrated
You don't have to repeat and reimplement the basic methods and attributes.
You only need to implement the unique features of your class.

I don't know if you understand it.

For more information, see my summary:
[Arrangement] basic object-oriented knowledge: Class, Object, and Instance)

(No post address is provided here. You can find the post address by searching the post title on google)

In python, how does one call the methods of the base class in the inheritance class?

Where is the _ init _ constructor. What do you want to call the parent class method. Take Class --..
Give you a comprehensive
Class Q (object ):
Def _ init _ (self ):
Self. x = False
Self. y = True

>>> Q = Q ()
>>> Class W (Q ):
Def _ init _ (self ):
# In this way, the x and y attributes of the parent class Q are inherited.
Q. _ init _ (self)
>>> W = W ()
>>> Print w. x, w. y

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.