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

相关推荐
kszlgy21 小时前
Day 52 神经网络调参指南
python
wrj的博客1 天前
python环境安装
python·学习·环境配置
Pyeako1 天前
深度学习--BP神经网络&梯度下降&损失函数
人工智能·python·深度学习·bp神经网络·损失函数·梯度下降·正则化惩罚
摘星编程1 天前
OpenHarmony环境下React Native:Geolocation地理围栏
python
充值修改昵称1 天前
数据结构基础:从二叉树到多叉树数据结构进阶
数据结构·python·算法
q_35488851531 天前
AI大模型:python新能源汽车推荐系统 协同过滤推荐算法 Echarts可视化 Django框架 大数据毕业设计(源码+文档)✅
大数据·人工智能·python·机器学习·信息可视化·汽车·推荐算法
Yeats_Liao1 天前
开源生态资源:昇腾社区ModelZoo与DeepSeek的最佳实践路径
python·深度学习·神经网络·架构·开源
被星1砸昏头1 天前
掌握Python魔法方法(Magic Methods)
jvm·数据库·python
love530love1 天前
彻底解决 ComfyUI Mixlab 插件 Whisper.available False 的报错
人工智能·windows·python·whisper·win_comfyui
不解风水1 天前
《深度学习入门:基于 Python 的理论与实现》(斋藤康毅)
人工智能·python·深度学习