首页 > 代码库 > Rectangles
Rectangles
Sometimes it is obvious what the attributes of an object should be, but other times you have to make decisions. For examples, imagine you are designing a class to represent rectangles. What attributes would you use to specify the location and size of a rectangle? You can ignore angle; to keep things simple, assume that the rectangle is either vertical or horizontal.
There are at least two possibilities:
- You could specify one corner of the rectangle (or the center), the width, and the height.
- You could specify two opposing corners.
At this point it is hard to say whether either is better than the other, so we’ll implement the first one, just as an example.
import mathclass Point: """represents a point in 2-D space"""class Rectangle: """represent a rectangle. attributes: width, height, corner. corner is a Point object that specifies the lower-left corner. """box = Rectangle()box.width = 100.0box.height = 200.0box.corner = Point()box.corner.x = 0.0box.corner.y = 0.0
The figure shows the state of this object:
from Thinking in Python
Rectangles
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。