我正在尝试编写一个带
的类表面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()吗?
答案 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功能”不会在这里添加任何内容