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

相关推荐
孟健8 小时前
Karpathy 用 200 行纯 Python 从零实现 GPT:代码逐行解析
python
码路飞10 小时前
写了个 AI 聊天页面,被 5 种流式格式折腾了一整天 😭
javascript·python
曲幽12 小时前
FastAPI压力测试实战:Locust模拟真实用户并发及优化建议
python·fastapi·web·locust·asyncio·test·uvicorn·workers
敏编程17 小时前
一天一个Python库:jsonschema - JSON 数据验证利器
python
前端付豪17 小时前
LangChain记忆:通过Memory记住上次的对话细节
人工智能·python·langchain
databook17 小时前
ManimCE v0.20.1 发布:LaTeX 渲染修复与动画稳定性提升
python·动效
花酒锄作田1 天前
使用 pkgutil 实现动态插件系统
python
前端付豪1 天前
LangChain链 写一篇完美推文?用SequencialChain链接不同的组件
人工智能·python·langchain
曲幽1 天前
FastAPI实战:打造本地文生图接口,ollama+diffusers让AI绘画更听话
python·fastapi·web·cors·diffusers·lcm·ollama·dreamshaper8·txt2img
老赵全栈实战1 天前
Pydantic配置管理最佳实践(一)
python