2024/5/13
所花时间:1小时
代码行:70行
博客量:1篇
了解到的知识点:
import math
# 三维图形功能接口
class Shape3D:
def get_perimeter(self):
pass
def get_area(self):
pass
def get_volume(self):
pass
# 点类
class Point:
def _init_(self, x, y):
self.x = x
self.y = y
def get_coordinates(self):
return self.x, self.y
def set_coordinates(self, x, y):
self.x = x
self.y = y
def display(self):
print(f"Point coordinates: ({self.x}, {self.y})")
# 圆类
class Circle(Point, Shape3D):
def _init_(self, x, y, radius):
super()._init_(x, y)
self.radius = radius
def get_perimeter(self):
return 2 * math.pi * self.radius
def get_area(self):
return math.pi * self.radius ** 2
def display(self):
super().display()
print(f"Radius: {self.radius}")
# 球类
class Sphere(Circle):
def get_volume(self):
return (4/3) * math.pi * self.radius ** 3
# 圆柱类
class Cylinder(Circle, Shape3D):
def _init_(self, x, y, radius, height):
super()._init_(x, y, radius)
self.height = height
def get_area(self):
return 2 * math.pi * self.radius * (self.radius + self.height)
def get_volume(self):
return math.pi * self.radius ** 2 * self.height
# 圆锥类
class Cone(Circle, Shape3D):
def _init_(self, x, y, radius, height):
super()._init_(x, y, radius)
self.height = height
def get_volume(self):
return (1/3) * math.pi * self.radius ** 2 * self.height
# 测试类的创建和计算
circle1 = Circle(0, 0, 5)
sphere1 = Sphere(0, 0, 5)
cylinder1 = Cylinder(0, 0, 5, 10)
cone1 = Cone(0, 0, 5, 10)
print("Circle Information:")
circle1.display()
print(f"Perimeter: {circle1.get_perimeter()}")
print(f"Area: {circle1.get_area()}\n")
print("Sphere Information:")
sphere1.display()
print(f"Volume: {sphere1.get_volume()}\n")
print("Cylinder Information:")
cylinder1.display()
print(f"Area: {cylinder1.get_area()}")
print(f"Volume: {cylinder1.get_volume()}\n")
print("Cone Information:")
cone1.display()
print(f"Volume: {cone1.get_volume()}")