Python __init_;()正好接受4个参数(给定3个)
点通过给出x和y坐标来定义。两个点之间的距离可以使用距离法计算。点应至少具有以下特性x、y和距离_,可公开使用 圆是通过给定圆心和半径来定义的。圆有一种方法,即“是否在圆内”,用于回答给定点是否在圆内。圆应至少具有以下特性:圆心、半径和内部 这里的问题是我不能初始化我的父类和基类 旁注:功能和课程距离,圆圈,圆圈。在里面只需要一个pPython __init_;()正好接受4个参数(给定3个),python,Python,点通过给出x和y坐标来定义。两个点之间的距离可以使用距离法计算。点应至少具有以下特性x、y和距离_,可公开使用 圆是通过给定圆心和半径来定义的。圆有一种方法,即“是否在圆内”,用于回答给定点是否在圆内。圆应至少具有以下特性:圆心、半径和内部 这里的问题是我不能初始化我的父类和基类 旁注:功能和课程距离,圆圈,圆圈。在里面只需要一个p import math class Point(): def __init__(self, xpoint, ypoint): self.xp
import math
class Point():
def __init__(self, xpoint, ypoint):
self.xpoint = xpoint
self.ypoint = ypoint
def distance_from(xpoint, ypoint):
distance = math.sqrt(((p1.xpoint-p2.xpoint)**2)+((p1.ypoint-p2.ypoint)**2))
print(distance)
class Circle(Point):
def __init__(self, xpoint, ypoint, r):
super(Circle, self).__init__(xpoint, ypoint)
self.r = r
def is_inside(xpoint, ypoint):
if self.r > distance:
print("False")
else:
print("True")
p1 = Point(0, 0)
p2 = Point(2, 4)
p1.distance_from(p2)
circle = Circle(p2,4)
circle.is_inside(p1)
手动调用
\uuuu init\uuuu
时,您必须提供self
参数。将您的coe更改为:我为您的错误添加了注释
import math
class Point(object): # Add object here
def __init__(self, xpoint, ypoint):
self.xpoint = xpoint
self.ypoint = ypoint
def distance_from(self, xpoint, ypoint): # Missing self here
distance = math.sqrt(((p1.xpoint-p2.xpoint)**2)+((p1.ypoint-p2.ypoint)**2))
print(distance)
class Circle(Point):
def __init__(self, xpoint, ypoint, r):
super(Circle, self).__init__(xpoint, ypoint)
self.r = r
def is_inside(self, point): # Missing self here
if self.r > self.distance_from(point.xpoint, point.ypoint): # You'll have to callculate distance here
print("False")
else:
print("True")
p1 = Point(0, 0)
p2 = Point(2, 4)
p1.distance_from(p1.xpoint, p1.ypoint) # Missing points
c = Circle(p1.xpoint, p1.ypoint, 4) # Missing points
c.is_inside(p2)
如果不想使用super()。init请使用以下命令:
Point.__init__(self, xpoint, ypoint) # Change super to the superclass name. Add self to the init method
一些注意事项:
- 成员函数始终将self作为第一个参数
- 成员函数只能访问通过self或function参数访问的变量,而不能访问全局范围
import math class Point(object): def __init__(self, xpoint, ypoint): self.xpoint = xpoint self.ypoint = ypoint # Here you where missing self and you were accessing p2 that exists in global scope. def distance_from(self, another_point): distance = math.sqrt(((self.xpoint-another_point.xpoint)**2)+((self.ypoint-another_point.ypoint)**2)) print(distance) class Circle(Point): def __init__(self, xpoint, ypoint, r): super(Circle, self).__init__(xpoint, ypoint) self.r = r def is_inside(self, another_point): # you forgot to calculate the distance if self.distance_from(another_point) < self.r: print("True") else: print("False") p1 = Point(0, 0) p2 = Point(2, 4) p1.distance_from(p2) #create a circle centered at 2,4 with radius 4 circle = Circle(2, 4, 4) circle.is_inside(p1) # returns "True"
导入数学 类点(对象): 定义初始化(self、xpoint、ypoint): self.xpoint=xpoint self.ypoint=ypoint #在这里,您将看到缺少self的地方,您正在访问全局范围中存在的p2。 def距离(自身,另一个点): 距离=数学sqrt((self.xpoint-other_point.xpoint)**2)+(self.ypoint-other_point.ypoint)**2)) 打印(距离) 类圆(点): 定义初始化(self,xpoint,ypoint,r): 超级(圆,自).\uuuu初始\uuuuuu(xpoint,ypoint) self.r=r def在内部(自身,另一个点): #你忘记计算距离了 如果自距离(另一个点)
self
已存在于方法中。该错误与其他参数有关。