언어 자료구조 알고리즘/프로그래밍 실습

[python] 캡슐화 실습 - 직사각형 클래스 정의하기

언제나휴일 2020. 10. 29. 15:07
반응형

다음과 같은 코드가 있다. 직사각형 클래스를 정의하시오.

class Point:#점(Point)
    def __init__(self,x=0,y=0):
        self.x = x
        self.y = y
#직사각형(Rectangle)
rectangle = Rectangle()
rectangle.position = Point(3,4)
rectangle.width =20
rectangle.height=40
print("면적:",rectangle.GetArea())

point = Point(10,10)
rect1 = Rectangle(point,20,30)
print("x:",rect1.position.x)
print("y:",rect1.position.y)
print("width:",rect1.width)
print("height:",rect1.height)
print("면적:",rect1.GetArea())

class Point:#점(Point)
    def __init__(self,x=0,y=0):
        self.x = x
        self.y = y
#직사각형(Rectangle)
class Rectangle:
    def __init__(self,position=Point(0,0),width=0,height=0):
        self.position = position
        self.width = width
        self.height = height
    def GetArea(self):
        return self.width*self.height

rectangle = Rectangle()
rectangle.position = Point(3,4)
rectangle.width =20
rectangle.height=40
print("면적:",rectangle.GetArea())

point = Point(10,10)
rect1 = Rectangle(point,20,30)
print("x:",rect1.position.x)
print("y:",rect1.position.y)
print("width:",rect1.width)
print("height:",rect1.height)
print("면적:",rect1.GetArea())
반응형