DAY 28

浙大疏锦行

##DAY 28

import math

题目1:圆类

class Circle:

def init (self, radius=1):

self.radius = radius

复制代码
def calculate_area(self):
    area = math.pi * self.radius ** 2
    return round(area, 2)

def calculate_circumference(self):
    circum = 2 * math.pi * self.radius
    return round(circum, 2)

题目2:长方形类

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

题目3:图形工厂函数

def create_shape(shape_type, *args):

if shape_type == "circle":

return Circle(*args)

elif shape_type == "rectangle":

return Rectangle(*args)

else:

raise ValueError("不支持的图形类型")

测试题目1

print("===== 测试圆 =====")

circle = Circle(5)

print(f"半径: {circle.radius}")

print(f"面积: {circle.calculate_area()}")

print(f"周长: {circle.calculate_circumference()}")

测试题目2

print("\n===== 测试长方形 =====")

rect = Rectangle(4, 6)

print(f"长: {rect.length}, 宽: {rect.width}")

print(f"面积: {rect.calculate_area()}")

print(f"周长: {rect.calculate_perimeter()}")

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

square = Rectangle(5, 5)

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

测试题目3 工厂函数

print("\n===== 测试图形工厂 =====")

shape1 = create_shape("circle", 5)

print(shape1.calculate_circumference())

shape2 = create_shape("rectangle", 3, 4)

print(shape2.is_square())

相关推荐
码云骑士1 小时前
71-Agent记忆系统-短期记忆-长期记忆-向量知识库三层架构
python·架构
卷无止境1 小时前
Python 的 exec 与 eval :动态代码执行的能力、风险与工程实践
后端·python
user-猴子1 小时前
从零构建 2048 游戏,解析“Python-Use”范式的完整闭环
开发语言·python·游戏
郝学胜_神的一滴1 小时前
Python 高级编程 025:二分利器bisect模块:优雅维系有序序列,极致优化检索性能
python·pycharm
qq_401700411 小时前
Qt容器性能优化:QVector、QHash、QMap到底应该怎么选?
开发语言·qt·性能优化
星栈独行2 小时前
Node 接口该写同步还是异步?
服务器·开发语言·后端·程序人生·node.js
小陈工2 小时前
第8篇:Flask轻量级框架与扩展生态深度解析(下)
后端·python·面试
这就是佬们吗2 小时前
Python入门③-运算符、条件与循环
开发语言·数据库·vscode·python·pycharm·编辑器
小刘学技术2 小时前
AI人工智能中的类别不平衡问题:成因、影响与解决方案
开发语言·人工智能·python·机器学习