class Rectangle - Python

时间:2017-02-01 19:42:54

标签: python class object

我刚刚开始使用面向对象的编程。我已经创建了一些类,我正在尝试完成矩形类。任何和所有的帮助表示赞赏。

当你需要引用self以及何时可以创建变量时,我很困惑。例如,在定义矩形的长度时,我不知道是否应该调用变量self.length或只是长度,并且我无法找到以这种方式定义的任何矩形类。

import math

class Point (object):
  # constructor 
  def __init__ (self, x = 0, y = 0):
    self.x = x
    self.y = y

  # get distance
  def dist (self, other):
    return math.hypot (self.x - other.x, self.y - other.y)

  # get a string representation of a Point object
  def __str__ (self):
    return '(' + str(self.x) + ", " + str(self.y) + ")"

  # test for equality
  def __eq__ (self, other):
    tol = 1.0e-16
    return ((abs (self.x - other.x) < tol) and (abs(self.y - other.y) < tol))

class Circle (object):
  # constructor
  def __init__ (self, radius = 1, x = 0, y = 0):
    self.radius = radius
    self.center = Point (x, y)

  # compute cirumference
  def circumference (self):
    return 2.0 * math.pi * self.radius

  # compute area
  def area (self):
    return math.pi * self.radius * self.radius

  # determine if point is strictly inside circle
  def point_inside (self, p):
    return (self.center.dist(p) < self.radius)

  # determine if a circle is strictly inside this circle
  def circle_inside (self, c):
    distance = self.center.dist (c.center)
    return (distance + c.radius) < self.radius

  # determine if a circle c intersects this circle (non-zero area of overlap)
  def does_intersect (self, c):

  # string representation of a circle
  def __str__ (self):

  # test for equality of radius
  def __eq__ (self, other):
    tol = 1.0e-16

class Rectangle (object):
  # constructor
  def __init__ (self, ul_x = 0, ul_y = 1, lr_x = 1, lr_y = 0):
    if ((ul_x < lr_x) and (ul_y > lr_y)):
      self.ul = Point (ul_x, ul_y)
      self.lr = Point (lr_x, lr_y)
    else:
      self.ul = Point (0, 1)
      self.lr = Point (1, 0)

  # determine length of Rectangle
  def length (self):

  # determine width of Rectangle
  def width (self):

  # determine the perimeter
  def perimeter (self):

  # determine the area
  def area (self):

  # determine if a point is strictly inside the Rectangle
  def point_inside (self, p)

  # determine if another Rectangle is inside this Rectangle
  def rectangle_inside (self, r):

  # determine if two Rectangles overlap
  def does_intersect (self, other):

  # determine the smallest rectangle that circumscribes a circle
  def rect_circumscribe (self, c):

  # give string representation of a rectangle
  def __str__ (self):

  # determine if two rectangles have the same length and width
  def __eq__ (self, other):

2 个答案:

答案 0 :(得分:0)

基本上,将值设置为self.length使您能够从类中的其他函数以及类外部访问此值。如果将值设置为length,则只能在类中的当前函数中访问此变量。

答案 1 :(得分:0)

只是一个开始,尝试继续自己:

class Rectangle (object):
  # constructor
  def __init__ (self, ul_x = 0, ul_y = 1, lr_x = 1, lr_y = 0):
    # Called if you say: my_rectancle = Rectangle (-10, 10, 10, -10)
    # Puts parameters in fields of your newly created object of class Rectancle
    self.ul_x = ul_x
    self.ul_y = ul_y
    self.lr_x = lr_x
    self.lr_y = lr_y

  # compute cirumference
  def circumference (self):
    return 2 * (self.ur_x - self.lr_x) + 2 * (self.ul_y - self.lr_y)

  # compute area
  def area (self):
    return (self.ur_x - self.lr_x) * (self.ul_y - self.lr_y)

[编辑]

关于评论中的其他代码,它与应有的内容非常接近。有一些更正:

# determine length of Rectangle
def length (self):
    return self.ul_y - self.lr_y

# determine width of Rectangle
def width (self):
    return self.lr_x - self.ul_x
    # self. has been added, since e.g. lr_x is not a parameter
    # of the width function, but a field of the object you make
    # by instantiation: 'rectangle = Rectangle (10, 20, 100, 200)'
    # After that, self refers to the object 'rectangle' you created,
    # which has class 'Rectangle'.
    #
    # Note that a class is a type.
    # You can have a type 'Dog'.
    # Dog 'fluffy' is an instance of that class, so a particular dog.
    # In the methods (functions) of 'Dog', 'self' refers to the particular
    # dog you're working with.

# determine the perimeter
def perimeter (self):
    return 2 * self.width () + 2 * self.length ()
    # Note the () after width and length.
    # They're needed because width and length are
    # function calls (they DO something) rather than fields (data)
    # You could also have stored width and length into fields,
    # just like the constructor did with ul_x, ul_y, lr_x and lr_y,
    # storing them in self.ul_x, self.ul_y etc.
    # Then the braces wouldn't have been needed.

    # But also out some superfluous braces here
    # Multiplication goes before addition anyhow
    # And return returns everything after it, no braces needed.       

# determine the area
def area (self):
    return self.width () * self.length ()

那么,你现在到底有多远?