python 3.6.3(OOPS)

时间:2017-12-14 09:08:14

标签: python oop

import math
class Circle():
    def __init__(self,radius,centre):
        self.radius=radius
        self.centre=centre

    def circumference():
        return 2.0*math.pi*self.radius 


c1 = Circle(1.0, (0.5 , 1.0))
c2 = Circle(1.5, (-0.5 , 2.0))

print(c1.radius)
print(c2.radius)
print(c1.circumference)
print(c2.circumference)

输出:

1.0
1.5
<bound method Circle.circumference of <__main__.Circle object at 0x7facb18214a8>>

这个错误是什么意思?

1 个答案:

答案 0 :(得分:0)

可能你忘记在circumference之后给出括号。由于circumferenceClass Circle的方法,因此您需要将其称为circumference()。您还需要在方法/函数self中传递circumference,以便在radius

内访问__init__(在circumference中定义)
import math
class Circle():
    def __init__(self,radius,centre):
        self.radius=radius
        self.centre=centre

    def circumference(self):
        return 2.0*math.pi*self.radius 


c1 = Circle(1.0, (0.5 , 1.0))
c2 = Circle(1.5, (-0.5 , 2.0))

print(c1.radius)
print(c2.radius)
print(c1.circumference())
print(c2.circumference())