1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59
| import math import turtle
class Dimension: def __init__(self, x, y): self.x = x self.y = y
def area(self): pass
def draw(self): pass
class Circle(Dimension): def __init__(self, x, y, radius): super().__init__(x, y) self.radius = radius
def area(self): return math.pi * self.radius**2
def draw(self): pass
class Rectangle(Dimension): def __init__(self, x, y, length, width): super().__init__(x, y) self.length = length self.width = width
def area(self): return self.length * self.width
def draw(self): turtle.penup() turtle.goto(self.x - self.length / 2, self.y - self.width / 2) turtle.pendown() for _ in range(2): turtle.forward(self.length) turtle.left(90) turtle.forward(self.width) turtle.left(90)
circle = Circle(0, 0, 50) print(f"圆的半径为{circle.radius}圆的面积为{circle.area()}")
rectangle = Rectangle(0, 0, 60, 40) print(f"矩形的长为{rectangle.length},宽为{rectangle.width},面积为{rectangle.area()}")
turtle.speed(1) rectangle.draw() turtle.done()
|