首先感谢您抽出宝贵时间考虑以下问题。英语不是我的主要语言,所以我尽量保持清醒,所以我提前为此道歉。
考虑这个问题的代码(请记住我不能更改Point类中的任何代码):
class Point:
def __init__(self, xcoord=0, ycoord=0)
self.x = xcoord
self.y = ycoord
def setx(self, xcoord):
self.x = xcoord
def sety(self, ycoord):
self.y = ycoord
def __repr__(self):
return 'Point('+str(self.x)+','+str(self.y)+')'
def __str__(self):
return 'Point('+str(self.x)+','+str(self.y)+')'
class Figure:
def __init__(self, bottom_left, top_right):
Point.__init__(bottom_left)
Point.__init__(top_right)
self.bottom_left = bottom_left
self.top_right = top_right
def get_bottom_left(self):
print(Point().setx(self.bottom_left))
print (self.bottom_left)
def get_top_right(self):
print(self.top_right)
def __repr__(self):
return 'Point('+str(self.x)+','+str(self.y)+') + ', Point('+str(self.x)+','+str(self.y)+')'
def __str__(self):
return 'Point('+str(self.x)+','+str(self.y)+') + ', Point('+str(self.x)+','+str(self.y)+')'
分配时
f = Figure(Point(),Point(1,2))
1)据我所知, init , repr 和 str 是python方法,通过在类中修改对象而不是明确地叫。因此,两个类中 repr 和 str 的原因是能够获得输出" Point(0,0),Point()"在解释器中输入f时。但是,当我尝试时,我得到的图没有属性' x'。
a)我设法在将一个点直接用于Point类时创建x和y值,但是当我通过类图进行时我似乎无法做到这一点,即使我正在初始化bottom_left和top_right在类图中,通过使用
类的Point点。的初始化(BOTTOM_LEFT)
换句话说,我不知道如何将图(Point(),Point(1,2))分解为:
Point()= x = 0,y = 0
点(1,2)= x = 1,y = 2
通过使用第一类
2)我不知道如何覆盖Python的功能"不使用继承,它甚至可能吗?我指的是我在这里使用的方法: init , repr 和 str 。
3)尝试从类Point通过
访问函数setx()时print(Point().setx(self.bottom_left))
我得到的值为none。当我在setx()中返回一个返回(只是为了测试因为我不能这样做)时,它返回Point(0,0)而不是x值。
我的猜测是我必须使用继承,但我建议只使用Point类中的实例。
我经常在之前的另一篇帖子后经常问这个问题,但我现在已经没时间了,我真的想了解问题的核心(这是一个更简化的版本实际的分配让我能够攻击我的核心误解,这种误解通常将值,对象和实例从一个类互连到另一个类。)
答案 0 :(得分:1)
当您重新初始化Figure类的 init 方法中的点时,首先出现问题:
Point.__init__(bottom_left)
Point.__init__(top_right)
如果你检查'Point'类的 init 方法,它会输入参数'self,xcoord = 0,ycoord = 0'。所以你要做的是将'bottom_left'和'top_right'点作为'self'参数发送,使用x和y坐标的默认值。这会将这些点的值设置为0。
没有必要称这些初始化;当你调用Point()和Point(1,2)时,你已经初始化了这些点。
解决您的疑虑:
1)您正在使用Figure对象,而不是图 repr 和 str 方法中的Point对象。 'Figure'没有'x'和'y'属性,只有'bottom_left'和'top_right'(两个Point类型的对象)。所以请参考这些。
1a)上面说明:您不需要重新初始化Point对象。你只需要将'bottom_left'设置为输入的'bottom_left'对象,并将'top_right'设置为(你正在做的)。
2)通过在'Figure'中创建自己的 repr 和 str 方法,您将覆盖默认的'object'类方法,而不是'Point'方法。你可以利用Point的实现而不是重复一切。例如:
def __str__(self):
return str(self.bottom_left) + ', ' + str(self.top_right)
3)检查setx()的方法参数。它有什么期望?提示:不是Point对象!
答案 1 :(得分:1)
首先,您的代码中存在一些语法错误:
def __init__(self, xcoord=0, ycoord=0)
最后缺少:
。
return 'Point('+str(self.x)+','+str(self.y)+') + ', Point('+str(self.x)+','+str(self.y)+')'
return 'Point('+str(self.x)+','+str(self.y)+') + ', Point('+str(self.x)+','+str(self.y)+')'
在其中缺少或额外'
。
1)是的,这些方法改变了类实例的创建和表示方式。 __str__
和__repr__
失败,因为当它们被调用时(当您尝试print
该实例时),这两种方法下的代码失败。看起来你想要返回点坐标的值,更像是这样:
def __repr__(self):
return 'Point(' + str(self.bottom_left.x) + ',' + str(self.bottom_left.y) + '), ' + 'Point(' + str(self.top_right.x) + ',' + str(self.top_right.y) + ')'
def __str__(self):
return 'Point(' + str(self.bottom_left.x) + ',' + str(self.bottom_left.y) + '), ' + 'Point(' + str(self.top_right.x) + ',' + str(self.top_right.y) + ')'
a)您无需在Point.__init__
Figure
方法中明确调用__init__
;当你传递Point()
时就会发生这种情况。
2)不太清楚你在这里的意思,不确定如果不重写这些方法你会怎么做。
3)在Point()
被叫setx
后,你不打印setx
;你正在打印None
的回报。 setx
是从任何未明确返回内容的Python函数返回的内容。您的return
函数没有None
,因此返回p = Point()
p.setx(44)
print(p)
。它仍然有效,因为它仍然设置x。尝试:
if(n == 0 || m == 0) {
System.out.print("");
}