-
定义一个矩形类,拥有属性:长、宽 拥有方法:求周长、求面积
class rectangle: def __init__(self, length, width): self.length = length self.width = width def girth(self): return 2 * (self.length + self.width) def area(self): return self.length * self.width juxing = rectangle(4, 6) print(juxing.girth()) print(juxing.area())
-
定义一个二维点类,拥有属性:x坐标、y坐标 拥有方法:求当前点到另外一个点的距离
class point:
def __init__(self,x,y):
self.x=x
self.y=y
def distance(self):
return (self.x**2+self.y**2)**0.5
dis=point(5,6)
print(dis.distance())
- 定义一个圆类,拥有属性:半径、圆心 拥有方法:求圆的周长和面积、判断当前圆和另一个圆是否外切
#定义一个圆
class circle1:
pi = 3.14
def __init__(self, r, x: tuple):
self.r = r
self.x = x
def round(self):
return 2 * circle1.pi * self.r
def area(self):
return circle1.pi * self.r ** 2
circle = circle1(5, (3, 4))
print(circle.round())
print(circle.area())
#判断是否外切
class circle2(circle1):
pass
circle3 = circle2(6, (6, 7))
print(circle3.area())
print(circle3.round())
print(circle.x)#只为查看第一个圆的半径
print(circle.r)#只为查看第一个圆的圆心
print(circle3.r)#只为查看第二个圆的半径
print(circle3.x)#只为查看第二个圆的圆心
distance=((circle.x[0]-circle3.x[0])**2+(circle.x[1]-circle3.x[1])**2)**0.5
# print(distance)
if distance==circle.x+circle3.x:
print("两圆外切")
else:
print("两圆不外切")
-
定义一个线段类,拥有属性:起点和终点, 拥有方法:获取线段的长度
class line: def __init__(self, start: tuple, end: tuple): self.start = start self.end = end def length(self): return int(((self.start[0] - self.end[0]) ** 2 + (self.start[1] - self.end[1]) ** 2) ** 0.5) line1 = line((5, 6), (4, 5)) print(line1.length())