首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >类Rectangle - Python

类Rectangle - Python
EN

Stack Overflow用户
提问于 2017-02-02 03:42:54
回答 2查看 1.7K关注 0票数 0

我刚刚开始使用面向对象编程。我已经创建了一些类,并且正在尝试完成rectangle类。任何和所有的帮助都是感激的。

我对什么时候需要引用self,什么时候可以创建变量感到困惑。例如,在定义矩形的长度时,我不知道应该调用变量self.length还是只调用长度,而且我还没有找到以这种方式定义的任何矩形类。

代码语言:javascript
复制
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):
EN

回答 2

Stack Overflow用户

发布于 2017-02-02 03:45:28

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

票数 0
EN

Stack Overflow用户

发布于 2017-02-02 03:59:48

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

代码语言:javascript
复制
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)

编辑

关于注释中的附加代码,它非常接近它应该是的样子。有一些更正:

代码语言:javascript
复制
# 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 ()

那么,你现在走了多远?

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/41988318

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档