1.编写一个圆类,圆类有半径Radius和圆心心center等属性,这些属性可以查看get 也可以重新设置set,另外还可以统计圆的个数,返回其面积及周长。 2.编写一个圆环类 Ring。圆环类有内半径innerRadius,外半径outerRadius和圆心center等属性,这些属性可以查看get 也可以重新设置set,另外,圆环还可以返回其面积及外周长。 3.创建圆环类的对象,并输出对象的面积和周长;创建多个圆,输出总的个数,每个圆的面积周长等。
时间: 2023-05-19 13:06:31 浏览: 96
1. 圆类的代码如下:
```python
class Circle:
def __init__(self, radius, center):
self.radius = radius
self.center = center
Circle.count += 1
count = 0
@property
def radius(self):
return self._radius
@radius.setter
def radius(self, value):
if value < 0:
raise ValueError("Radius cannot be negative")
self._radius = value
@property
def center(self):
return self._center
@center.setter
def center(self, value):
self._center = value
def area(self):
return 3.14 * self.radius ** 2
def perimeter(self):
return 2 * 3.14 * self.radius
```
2. 圆环类的代码如下:
```python
class Ring:
def __init__(self, inner_radius, outer_radius, center):
self.inner_radius = inner_radius
self.outer_radius = outer_radius
self.center = center
Ring.count += 1
count = 0
@property
def inner_radius(self):
return self._inner_radius
@inner_radius.setter
def inner_radius(self, value):
if value < 0:
raise ValueError("Inner radius cannot be negative")
self._inner_radius = value
@property
def outer_radius(self):
return self._outer_radius
@outer_radius.setter
def outer_radius(self, value):
if value < 0:
raise ValueError("Outer radius cannot be negative")
self._outer_radius = value
@property
def center(self):
return self._center
@center.setter
def center(self, value):
self._center = value
def area(self):
return 3.14 * (self.outer_radius ** 2 - self.inner_radius ** 2)
def perimeter(self):
return 2 * 3.14 * (self.outer_radius + self.inner_radius)
```
3. 创建圆环类的对象并输出面积和周长的代码如下:
```python
ring = Ring(3, 5, (0, 0))
print("Ring area:", ring.area())
print("Ring perimeter:", ring.perimeter())
```
创建多个圆并输出总个数、每个圆的面积和周长的代码如下:
```python
c1 = Circle(2, (0, 0))
c2 = Circle(3, (1, 1))
c3 = Circle(4, (2, 2))
print("Total circles:", Circle.count)
print("Circle 1 area:", c1.area())
print("Circle 1 perimeter:", c1.perimeter())
print("Circle 2 area:", c2.area())
print("Circle 2 perimeter:", c2.perimeter())
print("Circle 3 area:", c3.area())
print("Circle 3 perimeter:", c3.perimeter())
```
阅读全文