- #-*-coding= utf-8-*-
- # The syntax rules for file encoding definitions are: coding[:=]/s* ([-/w.] +)
- # no encoding specified will default to: ASCII
- # also be aware that the encoding of the physical file must conform to the encoding specified in this language
- # More reference: http://python.org/dev/peps/pep-0263/
- Class CBase:
- "'Resource Related class ' ' # passed cbase.__doc__ or class instance. __doc__ can output this description
- Counter = 0; # This is a variable belonging to a class, equivalent to a static variable of C + +, public
- __counter = 1; # variable with double underline, will be considered as private variable
- # cannot be accessed outside the class, otherwise it is considered to be a public variable
- the __init__ in #在Python corresponds to the constructor of C + +.
- #在__init__中定义的是对象的变量, the equivalent of C + + common variables
- def __init__ (self,name):
- self.name = name # Ordinary object variable, public
- self.__private1 = 2 # Private normal variable, private
- print (' __init__ base ')
- # __del__ equivalent to C + + destructor
- def __del__ (self):
- print (' base destruct ')
- # Any one class method must have at least one parameter
- # This parameter is equivalent to C + + 's this pointer
- # This parameter must be the first parameter
- # Conventions usually write self
- def help (self):
- "HelpDoc" # method of doc, reference syntax [class name | instance]. Method name. __doc__
- print ('---base help begin---/n ',self.name) # Reference class common variables here
- print (cbase.__counter) # Even if a method of this class refers to its own static variable,
- # also need to add class name or self-limit
- print (self.__private1) #私有变量只有本类方法有权限引用
- print ('---base help end---/n ')
- def Test (self):
- print (' Base Test ')
- # Inheritance of Classes
- # Multiple inheritance can be performed, Syntax class CC (CA,CB)
- Class Chigh (CBase):
- def __init__ (self):
- Cbase.__init__ (self,' high ') # When invoking the construction of a base class, to manually pass in the first parameter
- print (' __init__ high ')
- def __del__ (self):
- #CBase. __del__ (self), the destructor of the base class is not callable
- # destructors for the base class are not automatically called
- print (' High destruct ')
- # Inheriting a method of a class overrides the method with the same name as the base class
- def Test (self):
- print (' High Test ')
- # Python does not have the concept of method overloading
- # The last method defined in the source file will overwrite the previous method with the same name
- # Now in the call to test, the second parameter must be passed the name
- # The test method with one parameter above is overwritten.
- def Test (self,name):
- print (' High Test has arg: ', name)
- RH = Chigh ()
- #rh = CBase (' Hello ')
- Thh Help ()
- Thh Test ();
- Print (Cbase.counter)
- Print (Rh.name)
Python notes: Classes and inheritance