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

相关推荐
wuyk5553 小时前
第2章:六步换相原理全解+STM32工程实战
c语言·开发语言·stm32·单片机·嵌入式硬件
hey_sml3 小时前
JAVA每日一学---CompletableFuture中thenApply与thenCompose区别详解
java·开发语言
萧瑟余晖5 小时前
Java深入解析篇十七之SpringCloud
java·开发语言
北斗落凡尘5 小时前
LangGraph 入门实战(2)
python·langchain
ttod_qzstudio5 小时前
Java 常用语法极简通关(五):类与对象——字段、方法、构造器、this 与 static
java·开发语言·python
萧瑟余晖6 小时前
Java深入解析篇十七之Spring Security
java·开发语言·spring
jufeng13077 小时前
【系列:手搓自主 AI Agent:Hermes 架构原理剖析 · 第 1 篇】
人工智能·python·架构·agent
月光船幽幽7 小时前
影子模式下保护 logits 不被修改
人工智能·python·算法
二十雨辰9 小时前
[Java]-Spring面试题
java·开发语言
_oP_i10 小时前
python 后缀 mjs文件
开发语言·python