本文介绍了使用其他类的实例,覆盖和分离Point(x,y)中x和y的值,并将其从一个类转移到另一个类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

首先,感谢您抽出宝贵时间考虑以下询问。英语不是我的主要语言,所以我尽量保持清晰,因此我对此表示歉意。

请考虑以下代码(请记住,我无法更改Point类中的任何代码):

Considering this code for the questions (keeping in mind that I cannot change any of the code in the class 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)据我所知,初始化 repr str 是python方法,它们通过位于类中而无需显式调用即可修改对象。因此, repr str 都在这两个类中的原因是,在解释器中输入f时,能够获得输出 Point(0,0),Point() 。但是当我尝试得到时,Figure没有属性 x。

1) To my knowledge, init, repr and str are python methods that by being in a class they modify the object without being called explicitly. So the reason repr and str are in both classes is to be able to get the output "Point(0,0), Point()" when inputing f in the interpreter. But when I try that I get that Figure has no attribute 'x'.

a)当直接在类Point中使用单个点时,我设法创建了x和y值,但通过遍历时似乎无法做到这一点即使通过类Point通过使用

a) I manage to create the x and y values when using a single point directly into the class Point, but I can't seem to do that when doing it through class Figure even though I am initializing bottom_left and top_right in the class Figure, through the class Point by using

换句话说,我不知道如何分解Figure(Point(),Point( 1,2))放入:

In other words, I don't know how to break down Figure(Point(),Point(1,2)) into:

Point(1,2)= x = 1,y = 2

Point(1,2) = x = 1, y = 2

通过使用第一类

2)我不知道如何在不使用继承的情况下覆盖Python的函数,这有可能吗?此处,我指的是我在这里使用的方法: init repr str

2) I don't know how to "override a Python's function" without using Inheritance, is it even possible? By this I am referring to the methods I am using here: init, repr and str.

3)尝试通过

print(Point().setx(self.bottom_left))

我没有值。当我在setx()中放一个返回值(只是为了测试,因为我不能这样做)时,它返回Point(0,0)而不是x值。

I get the value none. When I put a return (just to test because I can't do this) in setx() it returns Point(0,0) instead of just the x value.

我的猜测是我必须使用继承,但是建议我只使用Point类中的实例。

My guess is that I have to use inheritance, but I was suggested to just use instances from class Point.

通常我不会在以前的另一篇文章中经常问这个问题,但是此时我已经没时间了,我真的想了解问题的核心(这是实际作业的简化版本让我能够解决我的核心误解,即通常将值,对象和实例从一个类互连到另一个类)。

I usually don't ask this often after another previous post, but I am running out of time at this point and I really want to understand the core of the problem (This is a more simplified version of the actual assignment for me to be able to attack my core misunderstandings which is in general interconnecting values, objects and instances from one class to another).

推荐答案

首先,您的代码中存在一些语法错误:

Firstly, you have a few syntax errors in your code:

def __init__(self, xcoord=0, ycoord=0)

缺少

is missing a : at the end.

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 该实例)这两种方法下的代码均失败。看起来您想返回点的坐标值,更像这样:

1) Yes, those methods change how the class instance is created and represented. __str__ and __repr__ are failing because when they're called (when you try to print that instance) the code under those two methods is failing. Looks like you want to return the value of the point's coordinates, more like this:

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 __ 在您的 __ init __ 方法中;当您通过它 Point()时就已经发生了。

a) You don't need to explicitly call Point.__init__ in your Figure __init__ method; that happens already when you pass it Point().

2)不太确定您的意思,不是

2) Not quite sure what you mean here, not sure how else you'd do it without overriding these methods.

3)在这里,您不是在打印 Point()调用 setx 之后;您正在打印 setx 的退货。 None 是任何未明确返回内容的Python函数返回的内容。您的 setx 函数没有返回,因此它返回 None 。它仍然起作用,因为它仍然设置了x。尝试:

3) Here you are not printing Point() after it's had setx called; you're printing the return of setx. None is what is returned from any Python function that doesn't explicitly return something. Your setx function doesn't have a return, so it returns None. It still works, in that it still sets x. Try:

p = Point()
p.setx(44)
print(p)

这篇关于使用其他类的实例,覆盖和分离Point(x,y)中x和y的值,并将其从一个类转移到另一个类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-29 23:26