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

相关推荐
mengge.cloud25 分钟前
Python基础保姆级!从入门到入土
开发语言·python·云原生·pygame
在爪洼山玩泥巴1 小时前
fastjson2 JSONPath 取不存在路径,为什么会抛 “not support”?
java·开发语言·jsonpath·fastjson
qq_158895411115 小时前
Suno-API 是什么?官方 V6 模型与 Python / TypeScript 接入指南
python·ai音乐·suno api
星落梦生11 小时前
SpringAOP基础笔记(7)
java·开发语言
曹牧13 小时前
Java:“刷新雪崩”
java·开发语言
郝学胜-神的一滴13 小时前
Effective STL 条款03:容器中的对象复制要廉价且正确
开发语言·数据结构·c++·算法·rpc·stl
西西弗Sisyphus14 小时前
C++ explicit 关键字工程实践
开发语言·c++
学无止境小袁同学15 小时前
Google ADK 学习指南
python·llm·agent·多agent·google adk·llmagent·agent cli
郝学胜-神的一滴15 小时前
Effective Python 条款35:用类封装替代生成器异常注入
开发语言·人工智能·python·金融·pandas·scikit-learn·matplotlib
feixing_fx16 小时前
原始类型与引用类型:赋值时到底复制了什么
开发语言·前端·javascript·前端框架