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())

相关推荐
alphaTao1 小时前
LeetCode 每日一题 2026/8/24-2026/8/30
python·算法·leetcode
苏灿烤鱼1 小时前
当 AI Agent 遇见真实科学环境:深度拆解 Scientific Agent Skills,把"聊天机器人"变成"AI 科学家"
python·开源·agent
张文君1 小时前
ubuntu26.04坏道坏块分区隔离急速版260831-V0.12
linux·python
Interview Aid1121 小时前
TikTok OA 四题分享|半小时内 AC,题目基本都是实现题
java·开发语言·算法·面试·职场和发展
CoderIsArt9 小时前
C#中UI 线程与 Dispatcher
开发语言·ui·c#
AI情绪识别开源11 小时前
检信 ALLEMOTION OS 加密打包可执行程序 — 全面测试报告版本: v1.3功能测试 / 性能测试 /
开发语言·数据结构·人工智能·功能测试
lupai12 小时前
手机在网状态接口实测效果与质量评估
大数据·python·智能手机·api接口
「QT(C++)开发工程师」12 小时前
C++ auto 用法详解
开发语言·c++
OPEN-F12 小时前
C++STL教程:容器适配器与实用工具
开发语言·c++