原始类节目:类蟒蛇错误
from Circle import circle
class circle:
def __init__(self,radius=1): #write def __init__(self,radius=1) to set a value
self.radius=radius
# constructer constructs the object and initializes it
def getArea(self):
return(3.142*self.radius*self.radius)
def getPerimeter(self):
return(2*3.142*self.myradius)
使用类:
def main():
c1=circle()
#If below happens
c1.radius=-1
#if above happens then negative value will be returned
c2=circle(5)
c3=circle(3)
print(c1.getArea())
print(c2.getArea())
print(c3.getArea())
main()
我只是想了解如何在Python类。当我运行该程序时,它说
builtins.AttributeError: 'circle' object has no attribute 'getArea'
我无法理解它为什么会发生。
你确定你的缩进是正确的吗?我在这篇文章中更正了缩进,假设你只是在如何发布时遇到了问题,但可能是'getArea()'方法不被看作'circle'类的一部分。另外,你为什么要导入'Circle.circle'然后用新类覆盖它呢? –
使用'math.pi'而不是自己定义pi。 –
你为什么要导入circle并重新定义它? –