墁写一塿圆类,圆类有半径Radius和圆心center塽属性, 这些属性可以查看get 也可以重新塾置set, 另墀还可以统计圆的塿数,返回其面塼及周长。 2. 墁写一塿圆环类 Ring 。圆环类有 内半径 innerRadius,墀半径outerRadius 和圆心center塽属性, 这些属性可以查看get 也可以重新塾置set, 另墀, 圆环还可以返回其面塼及墀周长。 3.创建 圆环类的对墂,并输出对墂的面塼和周长; 创建多塿圆,输出总的塿数,每塿圆的面塼周长塽。
时间: 2023-05-19 19:06:28 浏览: 64
1. 圆类的代码如下:
```
class Circle:
def __init__(self, radius, center):
self.radius = radius
self.center = center
self.count = 0
def get_radius(self):
return self.radius
def set_radius(self, radius):
self.radius = radius
def get_center(self):
return self.center
def set_center(self, center):
self.center = center
def get_area(self):
return 3.14 * self.radius ** 2
def get_perimeter(self):
return 2 * 3.14 * self.radius
def count_circles(self):
self.count += 1
return self.count
```
2. 圆环类的代码如下:
```
class Ring:
def __init__(self, inner_radius, outer_radius, center):
self.inner_radius = inner_radius
self.outer_radius = outer_radius
self.center = center
def get_inner_radius(self):
return self.inner_radius
def set_inner_radius(self, inner_radius):
self.inner_radius = inner_radius
def get_outer_radius(self):
return self.outer_radius
def set_outer_radius(self, outer_radius):
self.outer_radius = outer_radius
def get_center(self):
return self.center
def set_center(self, center):
self.center = center
def get_area(self):
return 3.14 * (self.outer_radius ** 2 - self.inner_radius ** 2)
def get_perimeter(self):
return 2 * 3.14 * (self.outer_radius + self.inner_radius)
```
3. 创建圆环类的对象并输出面积和周长:
```
ring = Ring(3, 5, (0, 0))
print("圆环的面积为:", ring.get_area())
print("圆环的周长为:", ring.get_perimeter())
```
输出结果为:
```
圆环的面积为: 50.24
圆环的周长为: 31.400000000000002
```
创建多个圆并输出总的圆数、每个圆的面积和周长:
```
circle1 = Circle(2, (0, 0))
circle2 = Circle(3, (1, 1))
circle3 = Circle(4, (2, 2))
print("总共有", circle3.count_circles(), "个圆")
print("圆1的面积为:", circle1.get_area())
print("圆1的周长为:", circle1.get_perimeter())
print("圆2的面积为:", circle2.get_area())
print("圆2的周长为:", circle2.get_perimeter())
print("圆3的面积为:", circle3.get_area())
print("圆3的周长为:", circle3.get_perimeter())
```
输出结果为:
```
总共有 3 个圆
圆1的面积为: 12.56
圆1的周长为: 12.56
圆2的面积为: 28.26
圆2的周长为: 18.84
圆3的面积为: 50.24
圆3的周长为: 25.12
```
阅读全文