skizziks_53

Python sub-class demo v1.0

Jan 6th, 2020
208
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 4.33 KB | None | 0 0
  1. '''
  2. January 5, 2020
  3.  
  4. Base class demonstration v 1.0
  5.  
  6. The reason to use a base class for your entire program is because then you create all your other classes inside that.
  7. When you create all the sub-classes, you pass a reference to the parent class to them. They all need to have that.
  8. If you do this, then any sub-class can call all the way back into the main parent class (of the application) and can also call into other sub-classes that are totally separate.
  9.  
  10. In this example:
  11. base_class is the main class for the whole program.
  12. MiniClass1 is a sub-class in the main class.
  13. MiniClass2 is a sub-class in the main class.
  14. MiniClass3 is a sub-class inside MiniClass2.
  15.  
  16. base_class has an int property named nValue1.
  17. When the program runs,
  18. MiniClass1 adds to nValue and then prints it.
  19. MiniClass2 adds to nValue and then prints it.
  20. MiniClass3 calls the methods in MiniClass1 (this is possible because even though the two classes are completely separate, they originate from the same base_class)
  21.  
  22.  
  23. (Note: I'm new at Python so there might be another way this would usually be done, but this demonstrates the point anyway)
  24. '''
  25.  
  26.  
  27. class MainApp1(object):
  28.     def __init__(self):
  29.         self.nValue1 = 0
  30.         self.child1 = MiniClass1(self) # Main class creates a local instance of MiniClass1
  31.         self.child2 = MiniClass2(self) # Main class creates a local instance of MiniClass2
  32.  
  33.     def print_parent_value(self):
  34.         print ("(from main class) nValue = " + str(self.nValue1)) # Main class can print the value of nValue, because it is a local property
  35.  
  36.  
  37. class MiniClass1(object):
  38.     def __init__(self, parent):
  39.         self.tParent = parent # The parent class instance is passed into the constructor and stored as tParent.
  40.  
  41.     def add_one_to_nValue(self):
  42.         self.tParent.nValue1 += 1 # MiniClass1 changes the value of the parent's property ( adds 1 to it )
  43.  
  44.     def print_parent_nValue(self):
  45.         print ("(from MiniClass1) MainApp1.nValue = " + str(self.tParent.nValue1)) # MiniClass1 prints the value of the parent's property
  46.  
  47.  
  48. class MiniClass2(object):
  49.     def __init__(self, parent):
  50.         self.tParent = parent # The parent class instance is passed into the constructor and stored as tParent.
  51.         self.child3 = MiniClass3(self) # MiniClass2 creates a local instance of MiniClass3.
  52.  
  53.     def add_two_to_nValue(self):
  54.         self.tParent.nValue1 += 2 # MiniClass2 changes the value of the parent's property ( adds 2 to it )
  55.  
  56.     def print_parent_nValue(self):
  57.         print ("(from MiniClass2) MainApp1.nValue = " + str(self.tParent.nValue1)) # MiniClass2 prints the value of the parent's property
  58.  
  59.  
  60. class MiniClass3(object):
  61.     def __init__(self, parent):
  62.         self.tParent = parent # The parent class instance is passed into the constructor and stored as tParent.
  63.  
  64.     def call_another_class_method1(self):
  65.         self.tParent.tParent.child1.add_one_to_nValue() # This method in Miniclass3 (that is inside MiniClass2) is calling a method that is in MiniClass1. (note the double reference to tParent here; the first tParent is the parent of MiniClass3, and the second tParent is the parent of the parent of MiniClass3)
  66.  
  67.     def call_another_class_method2(self):
  68.         self.tParent.tParent.child1.print_parent_nValue() # This method in Miniclass3 (that is inside MiniClass2) is calling a method that is in MiniClass1. (note the double reference to tParent here; the first tParent is the parent of MiniClass3, and the second tParent is the parent of the parent of MiniClass3)
  69.  
  70.  
  71.  
  72. def main():
  73.     base_class = MainApp1() # Parent class created here
  74.     base_class.print_parent_value() # Parent class nValue is zero
  75.     base_class.child1.add_one_to_nValue() # MiniClass1 adds 1 to parent's property
  76.     base_class.child1.print_parent_nValue() # MiniClass1 prints out value of parent's property
  77.     base_class.child2.add_two_to_nValue() # MiniClass2 adds 2 to parent's property
  78.     base_class.child2.print_parent_nValue() # MiniClass2 prints out value of parent's property
  79.  
  80.     base_class.child2.child3.call_another_class_method1() # This method in Miniclass3 (that is inside MiniClass2) is calling a method that is in MiniClass1.
  81.     base_class.child2.child3.call_another_class_method2() # This method in Miniclass3 (that is inside MiniClass2) is calling a method that is in MiniClass1.
  82.  
  83.  
  84. if __name__ == "__main__":
  85.     main()
Advertisement
Add Comment
Please, Sign In to add comment