1. 說(shuō)明:
- 共有兩個(gè).py文件,分別為Point.py和TestPoint.py
- 兩個(gè)文件位于相同的文件夾下
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
#計(jì)算兩點(diǎn)之間的距離
distance = math.sqrt(d_x**2 + d_y**2)
return distance
3. 文件TestPoint.py :
#調(diào)用Point.py文件中類Point
#from Point import Point
# p1 = Point()
# p2 = Point()
#導(dǎo)入Point類
import Point
p1 = Point.Point()
p2 = Point.Point()
p1.reset()
p2.move(6,8)
distan = p1.calculate_distance(p2)
print('兩點(diǎn)之間的距離為',distan)
輸出結(jié)果:
D:\python_project\venv\Scripts\python.exe D:/python_project/TestPoint.py
兩點(diǎn)之間的距離為 10.0
Process finished with exit code 0