创建曲面和矩形类

时间:2018-04-01 20:10:57

标签: python python-3.x

我正在尝试编写一个带

的类表面
  • self.rect - 这是一个矩形对象
  • __init__(self, filename, x, y, h, w) - 将filename字符串作为参数并将其保存到self.image实例变量
  • 采用x,y坐标集和h, w,并创建Rectangle
  • 中存储的self.rect对象
  • getRect - 返回矩形对象

class Surface:
    def __init__(self, filename, x, y, h, w):
        self.image = filename
        self.rect(x, y, h, w)
    def getRect(self):
        return Rectangle()

我导入的文件Rectangle.py是

class Rectangle:
    def __init__(self, x, y, h, w):
        self.x = x
        self.y = y
        self.h = h
        self.w = w
    def __str__(self):
        return "(x:)"+str(self.x) + ", y=" + str(self.y) + ", width:" + str(self.w) + ", height:" + str(self.h)

但它给了我这个错误:

    import Rectangle.py
ModuleNotFoundError: No module named 'Rectangle.py'; 'Rectangle' is not a package

因为Rectangle有init(self,x,y,h,w),所以我不必返回Rectangle()吗?

1 个答案:

答案 0 :(得分:1)

self.rect()不是Surface的函数,并且您的import语句不正确。

听起来你想要这个

from Rectangle import Rectangle

class Surface:
    def __init__(self, filename, x, y, h, w):
        self.image = filename
        self.rect = Rectangle(x, y, h, w)
    def getRect(self):
        return self.rect

但是,“getter功能”不会在这里添加任何内容