Language/Python

[Python] 클래스

sssbin 2022. 1. 20. 23:58

 

클래스와 객체

- 클래스로 만든 객체는 객체마다 고유한 성격을 가짐.

- 동일한 클래스로 만든 객체들은 서로 전혀 영향을 주지 않는다.

# 예시) 사칙연산 클래스

class FourCal:
    def setdata(self, first, second):
        self.first = first
        self.second = second

    def add(self):
        result = self.first + self.second
        return result

    def sub(self):
        result = self.first - self.second
        return result

    def mul(self):
        result = self.first * self.second
        return result

    def div(self):
        result = self.first / self.second
        return result

first, second, num = map(int, input().split())

cal = FourCal()
cal.setdata(first, second)

if num == 1:
    res = cal.add()
elif num == 2:
    res = cal.sub()
elif num == 3:
    res = cal.mul()
else:
    res = cal.div()

print(res)

 

생성자(Counstructor)

- 객체가 생성될 때 자동으로 호출되는 메서드

- __init__ 이용

class FourCal:
    def __init__(self, first, second):	# 생성자
        self.first = first
        self.second = second

    def setdata(self, first, second):
        self.first = first
        self.second = second

    def add(self):
        result = self.first + self.second
        return result

    def sub(self):
        result = self.first - self.second
        return result

    def mul(self):
        result = self.first * self.second
        return result

    def div(self):
        result = self.first / self.second
        return result

first, second, num = map(int, input().split())

cal = FourCal(first, second)	# 객체 생성 시 생성자의 매개변수에 해당되는 값을 전달해줘야 함

if num == 1:
    res = cal.add()
elif num == 2:
    res = cal.sub()
elif num == 3:
    res = cal.mul()
else:
    res = cal.div()

print(res)

 

클래스 상속

- 어떤 클래스를 만들 때 다른 클래스의 기능을 물려받을 수 있게 만드는 것

- class 클래스 이름(상속할 클래스 이름)

class FourCal:
    def __init__(self, first, second):
        self.first = first
        self.second = second

    def setdata(self, first, second):
        self.first = first
        self.second = second

    def add(self):
        result = self.first + self.second
        return result

    def sub(self):
        result = self.first - self.second
        return result

    def mul(self):
        result = self.first * self.second
        return result

    def div(self):
        result = self.first / self.second
        return result

class MoreFourCal(FourCal):	# FourCal을 상속
    def pow(self):
        result = self.first ** self.second
        return result

first, second, num = map(int, input().split())

cal = MoreFourCal(first, second)

if num == 1:
    res = cal.add()
elif num == 2:
    res = cal.sub()
elif num == 3:
    res = cal.mul()
else:
    res = cal.div()

print(res)
print(cal.pow())

 

메서드 오버라이딩

- 부모 클래스에 있는 메서드를 동일한 이름으로 다시 만드는 것

- 메서드를 오버라이딩하면 부모 클래스의 메서드 대신 오버라이딩한 메서드 호출

class FourCal:
    def __init__(self, first, second):
        self.first = first
        self.second = second

    def setdata(self, first, second):
        self.first = first
        self.second = second

    def add(self):
        result = self.first + self.second
        return result

    def sub(self):
        result = self.first - self.second
        return result

    def mul(self):
        result = self.first * self.second
        return result

    def div(self):
        result = self.first / self.second
        return result

class MoreFourCal(FourCal):
    def pow(self):
        result = self.first ** self.second
        return result
    
class SafeFourCal(FourCal):
    def div(self):	# 메서드 오버라이딩
        if self.second == 0:
            return 0
        else:
            return self.first / self.second

first, second, num = map(int, input().split())

cal = MoreFourCal(first, second)

if num == 1:
    res = cal.add()
elif num == 2:
    res = cal.sub()
elif num == 3:
    res = cal.mul()
else:
    res = cal.div()

print(res)
print(cal.pow())

 

클래스 변수

- 클래스로 만든 모든 객체에 공유됨.