如何使用类通过在__init__中接受用户输入来计算矩形的面积



"quot"正如你所看到的,我正试图使用类来找出矩形的面积,我想在其中接受用户输入,但第22行出现了错误"quot"从abc导入abc,abstractmethod#只是练习我今天学习的抽象方法

class shape(ABC):
@abstractmethod
def printdetail(self):
return 0
class Area(shape):
def __init__(self,length,breadth):
self.length = length
self.breadth = breadth
def printdetail(self):
return self.breadth * self.length
@classmethod
def userinput(cls):
length = int(input("enter a length")) #taking user input as length of rectangle
breadth = int(input("enter a breadth "))#taking user input as breadth of rectangle
rect= Area.userinput()
ans=rect.printdetail()
print(ans)

"quot"错误-ans=rect.printdetail((AttributeError:"NoneType"对象没有属性"printdetail"quot">

您的类方法正在获取输入,但从未创建要返回的Area实例。

@classmethod
def userinput(cls):
length = int(input("enter a length")) #taking user input as length of rectangle
breadth = int(input("enter a breadth "))#taking user input as breadth of rectangle
return cls(length, breadth)

相关内容

  • 没有找到相关文章