- 定义一个矩形类,拥有属性:长、宽 拥有方法:求周长、求面积
class Rectangle:
def __init__(self, long=0, width=0):
self.long = long
self.width = width
def size(self):
return self.long * self.width
def circumference(self):
return (self.long+self.width)*2
def __repr__(self):
return f'long:{self.long}, width:{self.width}'
- 定义一个二维点类,拥有属性:x坐标、y坐标 拥有方法:求当前点到另外一个点的距离
class Point:
def __init__(self, x_axis=0, y_axis=0):
self.x_axis = x_axis
self.y_axis = y_axis
def distance(self, x=0, y=0):
return ((x - self.x_axis) ** 2 + (y - self.y_axis) ** 2) ** 0.5
def __repr__(self):
return f'x1坐标:{self.x_axis}, y1坐标:{self.y_axis
- 定义一个圆类,拥有属性:半径、圆心 拥有方法:求圆的周长和面积、判断当前圆和另一个圆是否外切
class Circle:
pie = 3.1415926
def __init__(self, radius=0, x_center=0, y_center=0):
self.radius = radius
self.x_axis = x_center
self.y_axis = y_center
def circumference(self):
return Circle.pie * self.radius * 2
def size(self):
return Circle.pie * self.radius ** 2
def outcut(self, x, y, r):
if ((x-self.x_axis) ** 2 + (y-self.y_axis) ** 2) ** 0.5 == self.radius + r:
return '这两个圆外切'
else:
return '这两个圆不外切'
def __repr__(self):
return f'半径:{self.radius}, x坐标:{self.x_axis}, y坐标:{self.y_axis}'
- 定义一个线段类,拥有属性:起点和终点, 拥有方法:获取线段的长度
class Long:
def __init__(self, x1_axis=0, y1_axis=0, x2_axis=0, y2_axis=0):
self.x1_axis = x1_axis
self.y1_axis = y1_axis
self.y2_axis = y2_axis
self.x2_axis = x2_axis
def long(self):
return ((self.x1_axis - self.x2_axis) ** 2 + (self.y1_axis - self.y2_axis) ** 2) ** 0.5
def __repr__(self):
return f'x1坐标:{self.x1_axis}, y1坐标:{self.y1_axis}, x2坐标:{self.x2_axis}, y2坐标:{self.y2_axis}'