面向对象作业01
1. 定义一个矩形类,拥有属性:长、宽 拥有方法:求周长、求面积
class Square:
def __init__(self, long=0, width=0):
self.long = long
self.width = width
def perimeter(self):
p = (self.long + self.width) * 2
print(f'周长为:{p}')
def area(self):
print(f'面积为:{self.long * self.width}')
s1 = Square(10, 5)
s1.perimeter()
s1.area()
2. 定义一个二维点类,拥有属性:x坐标、y坐标 拥有方法:求当前点到另外一个点的距离
class Point:
def init(self, x, y):
self.x = x
self.y = y
def distanse(self, other):
return ((self.x - other.x) ** 2 + (self.y - other.y) ** 2) ** (1 / 2)
p1 = Point(10, 5)
p2 = Point(5, 1)
print(f’距离为:{p1.distanse(p2)}’)