Advertisement
go6odn28

inheritance_example

Feb 24th, 2024
93
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.05 KB | None | 0 0
  1. class First:
  2.     def __init__(self, a: int, b: int):
  3.         self.a = a
  4.         self.b = b
  5.         self.results = []
  6.  
  7.     def sum_numbers(self):
  8.         result = self.a + self.b
  9.         self.results.append(result)
  10.         return result
  11.  
  12.     def subtract_numbers(self):
  13.         result = self.a - self.b
  14.         self.results.append(result)
  15.         return result
  16.  
  17.  
  18. # Inheritance
  19. class Second(First):
  20.     pass
  21.  
  22.  
  23. class Third(Second):
  24.     pass
  25.  
  26.  
  27. class Fourth(Third):
  28.     pass
  29.  
  30.  
  31. print("----------First class----------")
  32. obj = First(5, 4)
  33. print(obj.sum_numbers())
  34. print(obj.subtract_numbers())
  35. print(obj.results)
  36.  
  37. print("----------Second class----------")
  38. obj2 = Second(3, 2)
  39. print(obj2.sum_numbers())
  40. print(obj2.subtract_numbers())
  41. print(obj2.results)
  42.  
  43. print("----------Third class----------")
  44. obj3 = Third(20, 10)
  45. print(obj3.sum_numbers())
  46. print(obj3.subtract_numbers())
  47. print(obj3.results)
  48.  
  49. print("----------Fourth class----------")
  50. obj4 = Fourth(10, 5)
  51. print(obj4.subtract_numbers())
  52. print(obj4.sum_numbers())
  53. print(obj4.results)
  54.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement