Refactor functions into Calculator class

- Add Calculator class with add and multiply methods
- Maintain original functions for compatibility
- Update test code to use class interface
This commit is contained in:
2026-03-04 18:29:53 +08:00
parent 9fe4e3a5c0
commit 40acd4ce82

27
test.py
View File

@@ -1,3 +1,14 @@
class Calculator:
"""A calculator class that provides basic arithmetic operations."""
def add(self, a, b):
"""Return the sum of two numbers."""
return a + b
def multiply(self, a, b):
"""Return the product of two numbers."""
return a * b
def add_two_numbers(a, b):
"""Return the sum of two numbers."""
return a + b
@@ -7,9 +18,17 @@ def multiply_two_numbers(a, b):
return a * b
if __name__ == "__main__":
# Example usage
sum_result = add_two_numbers(5, 3)
# Example usage with the Calculator class
calc = Calculator()
# Test addition using Calculator class
sum_result = calc.add(5, 3)
print(f"The sum is: {sum_result}")
product_result = multiply_two_numbers(5, 3)
print(f"The product is: {product_result}")
# Test multiplication using Calculator class
product_result = calc.multiply(5, 3)
print(f"The product is: {product_result}")
# Also test the original functions for compatibility
print(f"Function add result: {add_two_numbers(5, 3)}")
print(f"Function multiply result: {multiply_two_numbers(5, 3)}")