实现脚本
import math
class Distance:
def __init__(self):
print "Calculate the distance between two points"
def set_x1y1(self,x1,y1):
self.x1 = x1
self.y1 = y1
def set_x2y2(self,x2,y2):
self.x2 = x2
self.y2 = y2
def computation_result(self):
self.z = pow(pow(int(self.x1) - int(self.x2),2)+pow(int(self.y1) - int(self.y2),2),0.5)
def print_result(self):
print "the result is :" , self.z
a = Distance()
a.set_x1y1(raw_input('please input x1:'),raw_input('please input y1:'))
a.set_x2y2(raw_input('please input x2:'),raw_input('please input y2:'))
a.computation_result()
a.print_result()
结果:
注意的点:
- 类中函数定义了几个参数,对象赋值时就需要给几个用逗号隔开
def set_x1y1(self,x1,y1):
self.x1 = x1
self.y1 = y1
a.set_x1y1(3,4)
- self.x1默认是string类型,需要运用“-”的时候需要先转为int或者float。