1. 说明:
- 共有两个.py文件,分别为Point.py和TestPoint.py
- 两个文件位于相同的文件夹下
2. 文件Point.py :
import math class Point: def reset(self): self.x=0 self.y=0 def move(self,x,y): self.x = x self.y = y def calculate_distance(self,otherPoint): d_x = otherPoint.x - self.x d_y = otherPoint.y - self.y #计算两点之间的距离 distance = math.sqrt(d_x**2 + d_y**2) return distance
3. 文件TestPoint.py :
#调用Point.py文件中类Point #from Point import Point # p1 = Point() # p2 = Point() #导入Point类 import Point p1 = Point.Point() p2 = Point.Point() p1.reset() p2.move(6,8) distan = p1.calculate_distance(p2) print('两点之间的距离为',distan)
输出结果:
D:\python_project\venv\Scripts\python.exe D:/python_project/TestPoint.py 两点之间的距离为 10.0 Process finished with exit code 0