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

相关推荐
Minner-Scrapy14 小时前
Scrapy 2.17 源码解析:Scheduler 调度器与磁盘/内存双队列
java·爬虫·python·scrapy·网络爬虫·twisted
CODER030415 小时前
Anaconda和Mamba创建环境管理包常用命令合集
python·深度学习·conda·虚拟环境·mamba·常用命令大全
闲猫15 小时前
LangGraph / Capabilities / Fault tolerance
python·agent·langgraph
IvanCodes15 小时前
Python 基础语法(一):变量、数据类型与运算符
python
程序员三藏16 小时前
浅谈性能测试
自动化测试·软件测试·python·测试工具·职场和发展·测试用例·性能测试
l12586516 小时前
# RAG向量数据库优化实战:HNSW索引调参与生产级性能设计
数据库·python·mysql·langchain
meilindehuzi_a17 小时前
Python 基础语法(1):常量、变量、类型、输入输出与运算符
开发语言·python
Zane199417 小时前
调用了 async 函数却没执行?一文讲透协程、event loop 与 await
后端·python
OPEN-F17 小时前
Python进阶教程:装饰器与生成器深入
开发语言·python
zhipujiaoyu18 小时前
2026年大数据专科何去何从?就业前景、发展趋势全揭秘!
大数据·python