Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- '''
- January 5, 2020
- Base class demonstration v 1.0
- The reason to use a base class for your entire program is because then you create all your other classes inside that.
- When you create all the sub-classes, you pass a reference to the parent class to them. They all need to have that.
- 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.
- In this example:
- base_class is the main class for the whole program.
- MiniClass1 is a sub-class in the main class.
- MiniClass2 is a sub-class in the main class.
- MiniClass3 is a sub-class inside MiniClass2.
- base_class has an int property named nValue1.
- When the program runs,
- MiniClass1 adds to nValue and then prints it.
- MiniClass2 adds to nValue and then prints it.
- 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)
- (Note: I'm new at Python so there might be another way this would usually be done, but this demonstrates the point anyway)
- '''
- class MainApp1(object):
- def __init__(self):
- self.nValue1 = 0
- self.child1 = MiniClass1(self) # Main class creates a local instance of MiniClass1
- self.child2 = MiniClass2(self) # Main class creates a local instance of MiniClass2
- def print_parent_value(self):
- print ("(from main class) nValue = " + str(self.nValue1)) # Main class can print the value of nValue, because it is a local property
- class MiniClass1(object):
- def __init__(self, parent):
- self.tParent = parent # The parent class instance is passed into the constructor and stored as tParent.
- def add_one_to_nValue(self):
- self.tParent.nValue1 += 1 # MiniClass1 changes the value of the parent's property ( adds 1 to it )
- def print_parent_nValue(self):
- print ("(from MiniClass1) MainApp1.nValue = " + str(self.tParent.nValue1)) # MiniClass1 prints the value of the parent's property
- class MiniClass2(object):
- def __init__(self, parent):
- self.tParent = parent # The parent class instance is passed into the constructor and stored as tParent.
- self.child3 = MiniClass3(self) # MiniClass2 creates a local instance of MiniClass3.
- def add_two_to_nValue(self):
- self.tParent.nValue1 += 2 # MiniClass2 changes the value of the parent's property ( adds 2 to it )
- def print_parent_nValue(self):
- print ("(from MiniClass2) MainApp1.nValue = " + str(self.tParent.nValue1)) # MiniClass2 prints the value of the parent's property
- class MiniClass3(object):
- def __init__(self, parent):
- self.tParent = parent # The parent class instance is passed into the constructor and stored as tParent.
- def call_another_class_method1(self):
- 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)
- def call_another_class_method2(self):
- 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)
- def main():
- base_class = MainApp1() # Parent class created here
- base_class.print_parent_value() # Parent class nValue is zero
- base_class.child1.add_one_to_nValue() # MiniClass1 adds 1 to parent's property
- base_class.child1.print_parent_nValue() # MiniClass1 prints out value of parent's property
- base_class.child2.add_two_to_nValue() # MiniClass2 adds 2 to parent's property
- base_class.child2.print_parent_nValue() # MiniClass2 prints out value of parent's property
- base_class.child2.child3.call_another_class_method1() # This method in Miniclass3 (that is inside MiniClass2) is calling a method that is in MiniClass1.
- base_class.child2.child3.call_another_class_method2() # This method in Miniclass3 (that is inside MiniClass2) is calling a method that is in MiniClass1.
- if __name__ == "__main__":
- main()
Advertisement
Add Comment
Please, Sign In to add comment