赞
踩
我正在尝试用
Python学习面向对象的编程.为此,我需要创建一个计算线的斜率的方法,该线将原点连接到一个点. (我认为)我们假设原点是(0,0).例如:
Point(4, 10).slopeFromOrigin()
2.5
Point(12, -3).slopeFromOrigin()
-0.25
Point(-6, 0).slopeFromOrigin()
0
我们使用方程slope =(Y2-Y1)/(X2-X1)来计算斜率.此外,由于不允许除以0,因此当方法失败时,我们需要返回None.这是我试过的:
class Point:
#Point class for representing and manipulating x,y coordinates
def __init__(self, initX, initY):
#Create a new point at the given coordinates
self.x = initX
self.y = initY
def getX(self):
return self.x
def getY(self):
return self.y
def distanceFromOrigin(self):
return ((self.x ** 2) + (self.y ** 2)) ** 0.5
#define a method called slopeFromOrigin here
def slopeFromOrigin(self):
#set origin values for x and y (0,0)
self.x = 0
self.y
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。