Python notes: Classes and inheritance

Source: Internet
Author: User

  1. #-*-coding= utf-8-*-
  2. # The syntax rules for file encoding definitions are: coding[:=]/s* ([-/w.] +)
  3. # no encoding specified will default to: ASCII
  4. # also be aware that the encoding of the physical file must conform to the encoding specified in this language
  5. # More reference: http://python.org/dev/peps/pep-0263/
  6. Class CBase:
  7. "'Resource Related class ' ' # passed cbase.__doc__ or class instance. __doc__ can output this description
  8. Counter = 0; # This is a variable belonging to a class, equivalent to a static variable of C + +, public
  9. __counter = 1; # variable with double underline, will be considered as private variable
  10. # cannot be accessed outside the class, otherwise it is considered to be a public variable
  11. the __init__ in #在Python corresponds to the constructor of C + +.
  12. #在__init__中定义的是对象的变量, the equivalent of C + + common variables
  13. def __init__ (self,name):
  14. self.name = name # Ordinary object variable, public
  15. self.__private1 = 2 # Private normal variable, private
  16. print (' __init__ base ')
  17. # __del__ equivalent to C + + destructor
  18. def __del__ (self):
  19. print (' base destruct ')
  20. # Any one class method must have at least one parameter
  21. # This parameter is equivalent to C + + 's this pointer
  22. # This parameter must be the first parameter
  23. # Conventions usually write self
  24. def help (self):
  25. "HelpDoc" # method of doc, reference syntax [class name | instance]. Method name. __doc__
  26. print ('---base help begin---/n ',self.name) # Reference class common variables here
  27. print (cbase.__counter) # Even if a method of this class refers to its own static variable,
  28. # also need to add class name or self-limit
  29. print (self.__private1) #私有变量只有本类方法有权限引用
  30. print ('---base help end---/n ')
  31. def Test (self):
  32. print (' Base Test ')
  33. # Inheritance of Classes
  34. # Multiple inheritance can be performed, Syntax class CC (CA,CB)
  35. Class Chigh (CBase):
  36. def __init__ (self):
  37. Cbase.__init__ (self,' high ') # When invoking the construction of a base class, to manually pass in the first parameter
  38. print (' __init__ high ')
  39. def __del__ (self):
  40. #CBase. __del__ (self), the destructor of the base class is not callable
  41. # destructors for the base class are not automatically called
  42. print (' High destruct ')
  43. # Inheriting a method of a class overrides the method with the same name as the base class
  44. def Test (self):
  45. print (' High Test ')
  46. # Python does not have the concept of method overloading
  47. # The last method defined in the source file will overwrite the previous method with the same name
  48. # Now in the call to test, the second parameter must be passed the name
  49. # The test method with one parameter above is overwritten.
  50. def Test (self,name):
  51. print (' High Test has arg: ', name)
  52. RH = Chigh ()
  53. #rh = CBase (' Hello ')
  54. Thh Help ()
  55. Thh Test ();
  56. Print (Cbase.counter)
  57. Print (Rh.name)

Python notes: Classes and inheritance

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.