python,class问题请教

来源:百度知道 编辑:UC知道 时间:2024/05/30 19:13:58
额,这个是我的作业,刚刚开始学python,唉,实在是学不太会啊,好心人帮帮我吧。。题目是英文的哦。。

getBottomright() - return the bottom right corner as a pair of numbers.
move(p) - move the rectangle so that p becomes the top-left corner.
resize(width, height) - set the width and height of the rectangle to the supplied arguments.
__str__() - return the string representation of the rectangle as a pair of pairs - i.e. the top left and bottom right corners.

Example:

>>> r = rectangle((2,3), 5, 6)
>>> str(r)
'((2,3),(7,9))'
>>> r.move((5,5))
>>> str(r)
'((5, 5), (10, 11))'
>>> r.resize(1,1)
>>> str(r)
'((5, 5), (6, 6))'
>>> r.getBottomright()
(6, 6)

怎么样才可以打出example这样的结果啊?
还有class到底是什么东西。。
是要求写CLASS,初学就是不会,有什么好奇怪的,而且第一行都会写,就是里面的不会啊!!用pass带过也太那个什么点了吧。。至少给一个例子吧。。

你的题目里面并没有提到class啊

是不是要你建立一个rectangle类(class),在此类中实现上述函数和功能?我认为这个题目很简单;如果连这个都不会,你也有点儿忒......什么了点儿了吧?
基本结构:pass部分的内容就自己添吧

class rectangle(object):
_def __init__(self, topleft, long, high):
__pass
_def getBottomright(self):
__pass
_def move(self, a):
__pass
_def resize(self, long, high):
__pass
_def __str__(self):
__pass

===========================
完整的程序
可以直接运行,也可以import到运行环境中执行例子中的命令
这个程序没有进行类型错误检测,也就是用来学习而已

#!/usr/bin/python

class rectangle(object):
_def __init__(self, topleft, length, high):
__self.topleft = topleft
__self.length = length
__self.high = high

_def getBottomright(self):
__return (self.topleft[0]+self.length, self.topleft[1]+self.high)

_def move(self, topleft):
__self.topleft = topleft

_def resize(self, length, high):
__self.length = length
__self.high = high

_def