你知道如何在Python中输入同一类的对象吗?
我是Python初学者。
我必须定义一个将不同的点作为因子并返回自身与另一个点之间的距离的方法devantfromother()。
班级点用于将点(x,y)在二维平面作为对象处理。
这是我制作的代码。
import math
class Point:
def __init__(self, x=0, y=0):
self._x= x
self._y= y
def dFO(self, a, b):
self.a = a
self.b = b
otResult = math.sqrt(math.pow(self._x-self.a,2)+math.pow(self._y-self.b,2))
return otResult
def __str__(self):
return f"({self._x}, {self._y})"
我制作了两个对象A和b。
a = Point(1,1)
b = Point(2,3)
我必须使用方法DFO()来弄清A和B之间的距离。 我应该输入“ b”作为因素,但是我做了一种方法,以将点x,y的值放置。我不知道如何输入我作为因素的对象“ b”。
a.dFO(2,3)
a.dFO(b)
前者是我制作的,而后者是我想要做的。
如果您知道该怎么做,请帮助我!
I am python beginner.
I have to define a method distanceFromOther() that takes the different dot as a factor and returns the distance between itself and the other point.
Class Point is for treating points (x,y) in a two-dimensional plane as objects.
This is the code that I made.
import math
class Point:
def __init__(self, x=0, y=0):
self._x= x
self._y= y
def dFO(self, a, b):
self.a = a
self.b = b
otResult = math.sqrt(math.pow(self._x-self.a,2)+math.pow(self._y-self.b,2))
return otResult
def __str__(self):
return f"({self._x}, {self._y})"
I made two objects a and b.
a = Point(1,1)
b = Point(2,3)
I have to figure out the distance between a and b using method dFO().
I should input 'b' as factor, but I made the method to put the values of points x,y. I don't know how to input object 'b' that I made as the factor.
a.dFO(2,3)
a.dFO(b)
The former is what I made, and the latter is what I want to make.
If you know how to do it, please help me!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您只需要访问
_x
和_y
属性:另外,您无需将方法的参数转换为该对象的属性即可。
You just need to access the
_x
and_y
attributes:Also, you don't need to convert parameters of a method into attributes of that object.