2017-02-01 4 views
0

私はオブジェクト指向プログラミングの作業を始めています。私はいくつかのクラスを作成し、長方形クラスを完成しようとしています。すべての助けをいただければ幸いです。class Rectangle - Python

自分自身を参照する必要があるときと、変数を作成できるときは混乱します。たとえば、矩形の長さを定義する際に、変数self.lengthまたは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): 
+0

変数がメソッド内の一時的なプレースホルダ*である場合、 '' 'self'''を使う必要はありません。変数名をインスタンス属性にしたい場合は '' self'''を使います。 [クラスとインスタンス変数](https://docs.python.org/3/tutorial/classes.html#class-and-instance-variables) – wwii

答えて

0

基本的には、self.lengthに値を設定すると、あなたのクラス内とクラスの外から他の機能から、この値にアクセスすることができます。値をlengthに設定すると、クラス内の現在の関数でのみこの変数にアクセスできます。

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) 

[EDIT]

あなたのコメントでは、追加のコードに関しては、それがどうあるべきかに非常に近いです。いくつかの修正を加えました:

# 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

これは正しく見えますか? (lr_x - ul_x) リターン 位DEF周囲 を決定する: - #矩形 DEF幅の幅を決定する(自己) リターン(lr_y ul_y): –

+0

'#矩形 DEF長さ(自己)の長さを決定します周囲(自己):(self.width * self.length)」 –

+0

リターン: リターン((2 * self.width)+(2 * self.length)) 位領域 DEF領域(自己)を決定しますあなたは近くにいる、私の編集を参照してください。 –

関連する問題