码迷,mamicode.com
首页 > 其他好文 > 详细

Rectangles

时间:2014-09-25 17:09:17      阅读:290      评论:0      收藏:0      [点我收藏+]

标签:des   style   blog   http   color   io   os   ar   for   

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 math
class 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.0
box.height = 200.0
box.corner = Point()
box.corner.x = 0.0
box.corner.y = 0.0

The figure shows the state of this object:

 bubuko.com,布布扣                  

 

from Thinking in Python    

Rectangles

标签:des   style   blog   http   color   io   os   ar   for   

原文地址:http://www.cnblogs.com/ryansunyu/p/3992949.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!