【python基础知识】Day 28 类的定义和方法

知识点:
  1. 类的定义
  2. pass占位语句
  3. 类的初始化方法
  4. 类的普通方法
  5. 类的继承:属性的继承、方法的继承

作业

题目1:定义圆(Circle)类

要求:

1.包含属性:半径 radius。

2.包含方法:

  • calculate_area():计算圆的面积(公式:πr²)。
  • calculate_circumference():计算圆的周长(公式:2πr)。

3.初始化时需传入半径,默认值为 1。

import math
# 作业一定义圆类
class Circle:
    def __init__(self, radius = 1):
        self.radius = radius
 
    def calculate_area(self):
        s = math.pi * self.radius ** 2
        return s
    def calculate_circumference(self):
        c = 2 * math.pi * self.radius
        return c

# 测试输出
circle = Circle(5)
print(f"半径:{circle.radius}")       # 输出:半径:5
print(f"面积:{circle.calculate_area():.2f}")   # 输出:面积:78.54(保留两位小数)
print(f"周长:{circle.calculate_circumference():.2f}") # 输出:周长:31.42(保留两位小数)

题目2:定义长方形(Rectangle)类

1.包含属性:长 length、宽 width。

2.包含方法:

  • calculate_area():计算面积(公式:长×宽)。
  • calculate_perimeter():计算周长(公式:2×(长+宽))。 is_square() 方法,判断是否为正方形(长 == 宽)。

3.初始化时需传入长和宽,默认值均为 1。


class Rectangle:
    def __init__(self, length = 1, width = 1):
        self.length = length
        self.width = width
    def calculate_area(self):
        return self.width * self.length
 
    def calculate_perimeter(self):
        return 2 * (self.width + self.length)
 
    def is_square(self):
        return self.width ==self.length

# 测试输出
rect = Rectangle(4, 6)
print(f"长:{rect.length}, 宽:{rect.width}")  # 输出:长:4, 宽:6
print(f"面积:{rect.calculate_area()}")      # 输出:面积:24
print(f"周长:{rect.calculate_perimeter()}")  # 输出:周长:20
print(f"是否为正方形:{rect.is_square()}")    # 输出:是否为正方形:False
square = Rectangle(5, 5)
print(f"是否为正方形:{square.is_square()}")  # 输出:是否为正方形:True

题目3:图形工厂

创建一个工厂函数 create_shape(shape_type, *args),根据类型创建不同图形对象:图形工厂(函数或类)

shape_type="circle":创建圆(参数:半径)。

shape_type="rectangle":创建长方形(参数:长、宽)。

import math
class Circle:
    def __init__(self, radius = 1):
        self.radius = radius
 
    def calculate_area(self):
        s = math.pi * self.radius ** 2
        return s
    def calculate_circumference(self):
        c = 2 * math.pi * self.radius
        return c
circle = Circle(5)
print(f"半径:{circle.radius}")       # 输出:半径:5
print(f"面积:{circle.calculate_area():.2f}")   # 输出:面积:78.54(保留两位小数)
print(f"周长:{circle.calculate_circumference():.2f}") # 输出:周长:31.42(保留两位小数)
 
class Rectangle:
    def __init__(self, length = 1, width = 1):
        self.length = length
        self.width = width
    def calculate_area(self):
        return self.width * self.length
 
    def calculate_perimeter(self):
        return 2 * (self.width + self.length)
 
    def is_square(self):
        return self.width ==self.length
rect = Rectangle(4, 6)
print(f"长:{rect.length}, 宽:{rect.width}")  # 输出:长:4, 宽:6
print(f"面积:{rect.calculate_area()}")      # 输出:面积:24
print(f"周长:{rect.calculate_perimeter()}")  # 输出:周长:20
print(f"是否为正方形:{rect.is_square()}")    # 输出:是否为正方形:False
square = Rectangle(5, 5)
print(f"是否为正方形:{square.is_square()}")  # 输出:是否为正方形:True
 
 
def create_shape(shape_type, *args):
    if shape_type == 'circle':
        return Circle(args[0])
    elif shape_type == 'rectangle':
        return Rectangle(args[0], args[1])
    else:
        raise ValueError("shape type未定义")

# 测试输出
shape1 = create_shape("circle", 5)
print(shape1.calculate_circumference())  # 输出:31.42
 
shape2 = create_shape("rectangle", 3, 4)
print(shape2.is_square())                # 输出:False


 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值