“启用”类比较 [英] "Enabling" comparison for classes

查看:65
本文介绍了“启用”类比较的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望对我的CSC硬件有所帮助。它位于类/对象上,它是定义圆的简单类,名称为Circle(object)。

I would like some help on my CSC HW. It is on classes/objects, and it's a simple class on defining a circle, with the name class Circle(object).

硬件的确切文本(我完成了该硬件的前两部分,因此这第三部分是对初始问题的扩展):

The exact text of the HW (I completed the first two parts of this hw and thus this 3rd part is an expansion on the initial problem):

通过启用Circle的比较扩展您的Circle类使用<,>,> =,< =,==和!=等运算符的对象,如果一个圆实际上更大(即面积更大),则认为另一个圆比另一个圆更大 。

"""Expand on your Circle class by enabling the comparison of Circle objects using operators such as <, >, >=, <=, ==, and !=, where one Circle is considered "larger" than another if it is in fact larger (i.e., has greater area) the other Circle.

以下代码:

A = Circle(2, 5, 1.5)
B = Circle(-6, 1, 1)
print A < B, A != B, A >= B

应生成以下输出:

False True True

这是我的代码,用于显示圆的坐标和半径:

This is my code for displaying the coordinates and radius of the circle:

class Circle(object):
    def __init__(self, x=0, y=0, r=0):
        self.x = x
        self.y = y
        self.r = r
    def __str__(self):
        return "Circle at (%d , %d). Radius: %f" % (self.x, self.y, self.r)

def main():
    print Circle(3, 5, 4.0)

main()

此类的输出为在(3,5)处画圆。半径:4:000000

The output of this class is "Circle at (3 , 5). Radius: 4:000000"

我们被指向课本的某个页面,其中包含用于类的数学运算符:
eq ( ), gt (), ge (), lt (), le (), ne ()等。
所以我在想,我的教授想要这样吗?

We were pointed to a certain page of our textbook with math operators for classes: eq(), gt(), ge(), lt(), le(), ne(), etc. So I was thinking, did my professor want something like this?

import math
class Circle(object):
    def __init__(self, x=0, y=0, r=0):
        self.x = x
        self.y = y
        self.r = r
    def __str__(self):
        return "Circle at (%d , %d). Radius: %f" % (self.x, self.y, self.r)
    def calcArea(self, r):
        self.r = r
        return (math.pi)*(r**2)
    def __gt__(self, circ1Radius, circ2Radius)
        self.circ1Radius = circ1Radius
        self.circ2Radius = circ2Radius
        r1 = circ1Radius
        r2 = circ2Radius
        r1 > r2 or r2 > r1
    def __ge__(self, circ1Radius, circ2Radius)
    #And so on for __lt__(), __le__(), __ne__(), etc
def main():
    A = Circle(3,4,1.5) 
    B = Circle(1,2,5.0)
    C = Circle(5,7,7) 
    D = Circle(9,8,3)
    print A < B, B > C, A < C, A >= C
main()
#Output should be "True, False, True, False"

我们必须定义吗

推荐答案

定义或覆盖比较运算符,以便为类中的每个方法使用/ attribute?该类。 http://docs.python.org/reference/expressions.html#notin

Define or override the comparison operators for the class. http://docs.python.org/reference/expressions.html#notin

看起来就像在正确的轨道上,只是您只需要将第二个圆对象传递给您的比较即可。 self是第一个圆圈对象。所以self.r会给你第一个圆圈的r。另外,您还需要从该方法返回True或False。

Looks like you are on the right track, except you only need to pass the second circle object to your comparison. self refers to the first circle object. So self.r would give you the r of the first circle. Also you need to return True or False from the method.

def __gt__(self, circle2):
    return self.r > circle2.r

请注意,这只是比较圆的r。

Note that this is just comparing the r's of the circles.

这篇关于“启用”类比较的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆