본문 바로가기
프로그램

[파이썬] 문제 : 추상클래스 Polygon과 자식클래스 Rectangle

by 오디세이99 2023. 5. 27.
728x90
반응형

from abc import *                  # abc는 abstract base class의 약자
 
class Polygon(metaclass=ABCMeta):   # metaclass=ABCMeta를 지정.
    @abstractmethod                  # @abstractmethod 데코레이터
    def area(self):
        pass
 
    @abstractmethod
    def perimeter(self):
        pass

class Rectangle(Polygon):
    def __init__(self, width, height):
        self.width = width
        self.height = height
        
    def area(self):
        return self.width * self.height

    def perimeter(self):
        return (self.width * 2) + (self.height * 2)
    
rect = Rectangle(2.4, 4.3)
print('사각형 면적: %.2f'%rect.area())
print('가각형 둘레: %.2f'%rect.perimeter())

728x90
반응형

댓글