类的定义和方法作业:
|----------------------------------------------------------------------------------------------------------------------------------------------------------|------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| 01 02 03 04 05 06 07 08 09 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 | import math class Circle: ``def __init__(``self``, radius ``= 1``): ``self``.radius ``= radius ``def calculate_area(``self``): ``return math.pi ``* self``.radius ``*``* 2 ``def calculate_circumference(``self``): ``return 2 * math.pi ``* self``.radius circle ``= Circle(``5``) print``(f``"Radius: {circle.radius}"``) print``(f``"Area: {circle.calculate_area()}"``) print``(f``"Circumference: {circle.calculate_circumference()}"``) class Rectangle: ``def __init__(``self``, width ``= 1``, height ``= 1``): ``self``.width ``= width ``self``.height ``= height ``def calculate_area(``self``): ``return self``.width ``* self``.height ``def calculate_perimeter(``self``): ``return 2 * (``self``.width ``+ self``.height) ``def is_square(``self``): ``return self``.width ``=``= self``.height rect ``= Rectangle(``4``, ``6``) print``(f``"Width: {rect.width}, Height: {rect.height}"``) print``(f``"Area: {rect.calculate_area()}"``) print``(f``"Perimeter: {rect.calculate_perimeter()}"``) print``(f``"Is square: {rect.is_square()}"``) def create_shape(shape_type, ``*``args): ``if shape_type ``=``= "circle"``: ``return Circle(``*``args) ``elif shape_type ``=``= "rectangle"``: ``return Rectangle(``*``args) ``else``: ``raise ValueError(``"Unknown shape type"``) # 测试 create_shape 函数 shape1 ``= create_shape(``"circle"``, ``5``) print``(shape1.calculate_circumference()) shape2 ``= create_shape(``"rectangle"``, ``4``, ``6``) print``(shape2.is_square()) |