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>>
这个错误是什么意思?
答案 0 :(得分:0)
可能你忘记在circumference
之后给出括号。由于circumference
是Class 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())