Day34 PythonStudy

@浙大疏锦行

python 复制代码
import math

class Circle:
    def __init__(self, radius=1):
        """
        初始化圆形对象
        :param radius: 圆的半径,默认值为1
        """
        self.radius = radius
    
    def calculate_area(self):
        """
        计算圆的面积
        :return: 圆的面积 (π * r²)
        """
        return math.pi * self.radius ** 2
    
    def calculate_circumference(self):
        """
        计算圆的周长
        :return: 圆的周长 (2 * π * r)
        """
        return 2 * math.pi * self.radius
circle1 = Circle(5)
print(f"半径: {circle1.radius}")
print(f"面积: {circle1.calculate_area():.2f}")
print(f"周长: {circle1.calculate_circumference():.2f}")
print()

半径: 5

面积: 78.54

周长: 31.42

python 复制代码
class Rectangle:
    def __init__(self, length=1, width=1):
        """
        初始化长方形对象
        :param length: 长方形的长度,默认值为1
        :param width: 长方形的宽度,默认值为1
        """
        self.length = length
        self.width = width
    
    def calculate_area(self):
        """
        计算长方形的面积
        :return: 长方形的面积 (长 × 宽)
        """
        return self.length * self.width
    
    def calculate_perimeter(self):
        """
        计算长方形的周长
        :return: 长方形的周长 (2 × (长 + 宽))
        """
        return 2 * (self.length + self.width)
    
    def is_square(self):
        """
        判断是否为正方形
        :return: 如果是正方形返回 True,否则返回 False
        """
        return self.length == self.width

rect1 = Rectangle(4, 6)
print(f"长方形 - 长: {rect1.length}, 宽: {rect1.width}")
print(f"面积: {rect1.calculate_area()}")
print(f"周长: {rect1.calculate_perimeter()}")
print(f"是否为正方形: {rect1.is_square()}")


rect2 = Rectangle(5, 5)

print(f"是否为正方形: {rect2.is_square()}")

长方形 - 长: 4, 宽: 6

面积: 24

周长: 20

是否为正方形: False

是否为正方形: True

python 复制代码
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

class Rectangle:
    def __init__(self, length=1, width=1):
        self.length = length
        self.width = width
    
    def calculate_area(self):
        return self.length * self.width
    
    def calculate_perimeter(self):
        return 2 * (self.length + self.width)
    
    def is_square(self):
        return self.length == self.width

def create_shape(shape_type, *args):
    """
    工厂函数:根据形状类型创建对应的图形对象
    
    :param shape_type: 形状类型,支持 "circle" 或 "rectangle"
    :param args: 可变参数,用于传递形状的尺寸参数
        - 对于圆形:args[0] 为半径
        - 对于长方形:args[0] 为长,args[1] 为宽
    :return: 创建的图形对象,如果类型不支持则返回 None
    """
    if shape_type.lower() == "circle":
        if args:
            return Circle(args[0])
        else:
            return Circle()  # 使用默认半径
    
    elif shape_type.lower() == "rectangle":
        if len(args) == 2:
            return Rectangle(args[0], args[1])
        elif len(args) == 1:
            return Rectangle(args[0], args[0])  # 长和宽相同
        else:
            return Rectangle()  # 使用默认长宽
    
    else:
        print(f"错误:不支持的形状类型 '{shape_type}'")
        return None

shape1 = create_shape("circle",5)
print(shape1.calculate_circumference())

shape2 = create_shape("rectangle",3,4)
print(shape2.is_square())

31.41592653589793

False

相关推荐
麻雀飞吧7 分钟前
2026年期货量化入门路径:主流平台学习曲线与卡点观察
python
TechWayfarer7 分钟前
IP数据接口调用示例:社交软件如何做同城匹配与用户画像分析
python·网络协议·tcp/ip·社交电子
aqi0010 分钟前
15天学会AI应用开发(二)为什么编写提示词这么重要
人工智能·python·大模型·ai编程·ai应用
_Evan_Yao10 分钟前
线性代数 + 编程:用Python实现向量和矩阵运算
python·线性代数·矩阵
lili001222 分钟前
Claude自动修Bug配置优化与避坑指南
java·人工智能·python·bug·ai编程
Szime25 分钟前
靠谱的终端工厂采购电子元器件供应链哪家更适合研发型企业?
人工智能·python
2401_8734794030 分钟前
如何用IP离线库批量清洗订单IP,自动标注省市区?
开发语言·网络·python
py小王子31 分钟前
期刊复现 | Python实现扇形小提琴图
python·期刊图片复现
godspeed_lucip1 小时前
LLM和Agent——专题5: LLM Ops 入门(2)
人工智能·python
技术钱1 小时前
RAG 开发 6 个阶段优化策略分析
python