简体   繁体   English

上课圈子糟糕

[英]Oop for class circle

Im writing a class in python called Circle. 我用python写了一个叫做Circle的类。 Now as part of the class I want to define methods so I did but when I run the program it crashes and says they are not defined. 现在,作为类的一部分,我想像这样定义方法,但是当我运行程序时,它崩溃并说未定义它们。 I cant find the problem. 我找不到问题。

class Circle():
""" Holds data on a circle in the plane """
    def __init__(self,*args):
        if isinstance(args[0],Point) and isinstance(args[1],(float,int)):
            assert args[1]>0
            self.center= args[0]
            self.r= args[1]

        else:
            assert args[2]>0
            self.a=args[0]
            self.b=args[1]
            self.center= Point(self.a,self.b)
            self.r= args[2]
     def __mul__(self,other):
         assert isinstance(other,(float,int))
         assert other>0
         return Circle(self.center,self.r*other)

     __rmul__= __mul__
    def area(self):
        return math.pi*self.r**2

    def circumference(self):
        return 2*self.r*math.pi

    def move(self,p):
        assert isinstance(p,Point)
        self.center= p
        return None

I wrote a class for Point aswell, so thats not the problem. 我也为Point写了一个类,所以那不是问题。 This is what happens when I run the porgram: 这是我运行porgram时发生的情况:

>>> a=Circle(-3,-3,1)
>>> area(a)
Traceback (most recent call last):
File "<pyshell#4>", line 1, in <module>
area(a)
NameError: name 'area' is not defined

Edit : as @jsbueno points out, this was not the error causing your error message: Your indentation is off ( def __mul__ should be 1 space to the left), therefore Python thinks you have ended your class definition and are simply defining some more functions (not class methods). 编辑 :正如@jsbueno所指出的,这不是导致错误消息的错误: 缩进已关闭( def __mul__应该在左边留1个空格),因此Python认为您已经结束了类定义,并且只是在定义更多函数(不是类方法)。

Also, you should call area as a method - a.area() , not area(a) . 另外,您应该将area称为方法a.area() ,而不是area(a)

I've done a bit of reworking - added some comments, renamed some variables, generally cleaned up - this now works properly: 我做了一些重做-添加了一些注释,重命名了一些变量,通常进行了清理-现在可以正常使用:

from math import pi

class Point:
    def __init__(self, x, y):
        self.x = x
        self.y = y

class Circle:
    """
    Holds data on a circle in the plane
    """
    def __init__(self, a, b, c=None):
        if c is None:
            # Circle(Point, scalar)
            self.center = a
            self.r = b
        else:
            # Circle(scalar, scalar, scalar)
            self.center = Point(a, b)
            self.r = c

    @property
    def r(self):
        return self._r

    @r.setter
    def r(self, new_r):
        assert new_r > 0
        self._r = new_r

    def __mul__(self, scale_by):
        return Circle(self.center, self.r * scale_by)

    __rmul__ = __mul__

    def area(self):
        return pi * self.r**2

    def circumference(self):
        return 2 * pi * self.r

    def move(self, new_center):
        self.center = new_center

then 然后

a = Circle(-3,-3,1)
print(a.area())

gives

3.141592653589793

which is correct. 哪个是对的。

The methods in a class are available to the instance, but they have to be called as components of the instance with the "." 类中的方法可用于实例,但是必须将其作为实例的组件使用“。”来调用。 operator. 运营商。

So, in your example, you should use 因此,在您的示例中,您应该使用

a = Circle()
a.area()

and not 并不是

area(a)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM