|
| 1 | +import math |
| 2 | + |
| 3 | + |
| 4 | +class Point: |
| 5 | + def __init__(self, x = 0, y = 0): |
| 6 | + self.x = x # 데이터 속성(attribute) |
| 7 | + self.y = y |
| 8 | + |
| 9 | + def distance_from_origin(self): # 메서드 속성 |
| 10 | + return math.hypot(self.x, self.y) |
| 11 | + |
| 12 | + def __eq__(self, other): |
| 13 | + return self.x == other.x and self.y == other.y |
| 14 | + |
| 15 | + def __repr__(self): |
| 16 | + return "point ({0.x!r}, {0.y!r})".format(self) |
| 17 | + |
| 18 | + def __str__(self): |
| 19 | + return "({0.x!r}, {0.y!r})".format(self) |
| 20 | + |
| 21 | + |
| 22 | +class Circle(Point): |
| 23 | + def __init__(self, radius, x=0, y=0): |
| 24 | + super().__init__(x,y) # 생성 및 초기화 |
| 25 | + self.radius = radius |
| 26 | + |
| 27 | + def edge_distance_from_origin(self): |
| 28 | + return abs(self.distance_from_origin() - self.radius) |
| 29 | + |
| 30 | + def area(self): |
| 31 | + return math.pi*(self.radius**2) |
| 32 | + |
| 33 | + def circumference(self): |
| 34 | + return 2*math.pi*self.radius |
| 35 | + |
| 36 | + def __eq__(self, other): |
| 37 | + return self.radius == other.radius and super().__eq__(other) |
| 38 | + |
| 39 | + def __repr__(self): |
| 40 | + return "circle ({0.radius!r}, {0.x!r})".format(self) |
| 41 | + |
| 42 | + def __str__(self): |
| 43 | + return repr(self) |
| 44 | + |
| 45 | + |
| 46 | +# >>> import ShapeClass as shape |
| 47 | +# >>> a = shape.Point(3,4) |
| 48 | +# >>> a |
| 49 | +# point (3, 4) |
| 50 | +# >>> repr(a) |
| 51 | +# 'point (3, 4)' |
| 52 | +# >>> str(a) |
| 53 | +# '(3, 4)' |
| 54 | +# >>> a.distance_from_origin() |
| 55 | +# 5.0 |
| 56 | +# >>> c = shape.Circle(3,2,1) |
| 57 | +# >>> c |
| 58 | +# circle (3, 2) |
| 59 | +# >>> repr(c) |
| 60 | +# 'circle (3, 2)' |
| 61 | +# >>> str(c) |
| 62 | +# 'circle (3, 2)' |
| 63 | +# >>> c.circumference() |
| 64 | +# 18.84955592153876 |
| 65 | +# >>> c.edge_distance_from_origin() |
| 66 | +# 0.7639320225002102 |
0 commit comments