超级和子类

时间:2015-12-01 20:58:51

标签: python subclass superclass

我接到了一项任务,要求我们处理三个单独的课程:PointRectangleCanvas。我只是想知道是否有人可以帮我理解他们如何互动。编程语言是Python 3

以下是预期的输出:

>>>r1=Rectangle(Point(), Point(1,1), "red")
>>>r1
Rectangle(Point(0,0),Point(1,1),'red')

另一个例子是:

>>> r3=Rectangle(Point(), Point(2,1), "red")
>>> r3.get_perimeter()
6

1 个答案:

答案 0 :(得分:0)

如果您问自己,您将获得$a = array( 'book-1' => 'Book Title 1', 'book-2' => 'Book Title 2', 'book-3' => 'Book Title 3', 'book-4' => 'Book Title 4', 'book-5' => 'Book Title 5', 'book-6' => 'Book Title 6', 'book-7' => 'Book Title 7', 'book-8' => 'Book Title 8', 'book-9' => 'Book Title 9' ); $i = 1; $bookshelf = 1; foreach ( array_chunk($a, 4) as $section ) { echo '<div class="bookshelf-bottom">'; foreach( $section as $k => $v ) { // Output this for the first and last item. I'd recommend taking this out of the foreach or restructuring this somehow to avoid the manual check. if ($i == 9 || $i == 1) { echo '<div id="bookshelf-'.$bookshelf.'"></div>'; $bookshelf++; } $i++; // Output this book. echo '<div class="book-frame-'.$s.'">'.$v.'</div>'; } echo '</div>'; } 类型的对象与其他类型的对象之间的交互:需要定义A类型的对象是什么?

A的一种可能实现方式是通过两个相对的角定义矩形,这两个角是平面中的点,可以是Rectangle类型。 Point本身可以表示为一对数字。现在明显的定义是

Point

你可以定义

class Point():
    def __init__(self, x, y):
        self.xCoordinate = x
        self.yCoordinate = y



class Rectangle():
    def __init__(self, southwest, northeast, colour):
        self.bottomLeftCorner = southwest
        self.upperRightCorner = northeast
        self.fill = colour

按照上限定义,要定义rect = Rectangle(Point(0,1), Point(4,212), "red") ,需要两个Rectangle个对象和一个Point。不涉及超类/子类关系。

由于您没有提供String的任何示例,我无法帮助您,但我认为您可以自行完成。