Python 用类编写主驱动程序
我想Python 用类编写主驱动程序,python,python-3.x,Python,Python 3.x,我想 使用任何参数创建曲面对象 对曲面对象调用getRect()方法,并将结果矩形保存在变量中 并打印结果 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
- 使用任何参数创建曲面对象
- 对曲面对象调用
方法,并将结果矩形保存在变量中getRect()
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)
这是类的表面
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
这是我目前的主要功能
from Rectangle import Rectangle
from Surface import Surface
def main():
my_surface = Surface(2,4,2,4)
my_rectangle = my_surface.getRect()
print(my_surface.Rectangle)
()
要从类矩形中打印
\uuuuu str\uuuu
,而不显式调用\uuuuuuu str\uuuuu
函数,我将打印(my_surface.Rectangle)
,但它不打印任何内容。我应该写什么来代替代码?调用函数main()
<代码>()本身不起任何作用
你也没有给这个曲面一个文件名,也没有像my_Surface.Rectangle
这样的东西。因此,完整的代码应该如下所示:
from Rectangle import Rectangle
from Surface import Surface
def main():
my_surface = Surface("filename", 2, 4, 2, 4)
my_rectangle = my_surface.getRect()
print(my_rectangle)
main()
什么是my_serface?将my_rectangle=getRect(my_surface)更改为my_rectangle=my_surface.getRect()