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

相关推荐
天使day15 分钟前
FastAPI快速入门
python·fastapi
databook20 分钟前
用相关性分析消除“冗余特征”
python·机器学习·scikit-learn
幻想时空1 小时前
地图数据采集
python
梦想不只是梦与想1 小时前
Python 中的类型判断方法
python·type·isinstance
alphaTao2 小时前
LeetCode 每日一题 2026/7/27-2026/8/2
python·算法·leetcode
Kevin Wang7272 小时前
Nvidia-AGX-spark部署手册——课堂质量诊断(jetpack:r36)
python·docker·容器
Python私教3 小时前
Django 6.1 RC1 实测:FETCH_PEERS 两条 SQL 解决 N+1,select_related 还需要吗?
后端·python·django
玉鸯3 小时前
Agent 的任务编排:从 System Prompt 到 Hierarchical Multi-Agent
python·llm·agent
Python私教3 小时前
Django 6.0 自带 Tasks 到底能不能替代 Celery?跑完 3 组后台任务后我有答案了
后端·python·django
我的xiaodoujiao4 小时前
快速学习Python基础知识详细图文教程14--模块
开发语言·python·学习·测试工具