본문 바로가기
Python

[Python] 클래스

by 기리의 개발로그 2024. 8. 21.

클래스

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

import mod
a = mod.Fourcal()
a.setdata(4,2)
print(a.first)
print(a.second)

b = mod.Fourcal()
b.setdata(6,5)
print(b.first)
print(b.second)
4
2

6
5

메소드 선언의 self 매개변수에는 해당 메소드를 호출한 객체가 자동으로 전달된다.
따라서, 메소드 선언에는 매개변수가 3개지만 실제 호출시에는 2개 인수로만 호출한다.


class Fourcal():
    def setdata(self, first, second):
        self.first = first
        self.second = second
    def add(self):
        result = self.first + self.second
        return result
    def mul(self):
        result = self.first * self.second
        return result
    def sub(self):
        result = self.first - self.second
        return result
    def div(self):
        result = self.first / self.second
        return result
import mod
a = mod.Fourcal()
a.setdata(4,2)
print(a.add())
print(a.mul())
print(a.sub())
print(a.div())

b = mod.Fourcal()
b.setdata(6,5)
print(b.add())
print(b.mul())
print(b.sub())
print(b.div())
6
8
2
2.0

11
30
1
1.2

생성자


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 mul(self):
        result = self.first * self.second
        return result
    def sub(self):
        result = self.first - self.second
        return result
    def div(self):
        result = self.first / self.second
        return result

위와 같이 생성자함수는 __init__ 이 되며 객체 생성시에 자동으로 호출된다.
따라서, 변수 값을 셋팅하는 setdata() 함수를 호출하지 않아도 되며, 대신 객체 생성 시 생성자에 선언된 매개변수를 전달해줘야 한다.


import mod
a = mod.Fourcal(4, 2)
print(a.first)
print(a.second)
print(a.add())
print(a.mul())
print(a.sub())
print(a.div())
4
2
6
8
2
2.0

상속


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 mul(self):
        result = self.first * self.second
        return result
    def sub(self):
        result = self.first - self.second
        return result
    def div(self):
        result = self.first / self.second
        return result

class MoreFourcal(Fourcal):
    def pos(self):
        result = self.first ** self.second
        return result
import mod
a = mod.MoreFourcal(4, 2)
print(a.pos())
print(a.add())
16
6

class Fourcal():
    def __init__(self, first, second):
        self.first = first
        self.second = second
        print("Fourcal 생성자")
    def add(self):
        result = self.first + self.second
        print("Fourcal add : {0}".format(result))
    def mul(self):
        result = self.first * self.second
        print("Fourcal mul : {0}".format(result))
    def sub(self):
        result = self.first - self.second
        print("Fourcal sub : {0}".format(result))
    def div(self):
        result = self.first / self.second
        print("Fourcal div : {0}".format(result))
    def show_parent(self):
        print("부모클래스")

class MoreFourcal(Fourcal):
    def __init__(self, first, second, third):
        super().__init__(first, second)
        self.third = third
        print("MoreFourcal 생성자")
    def pos(self):
        result = (self.first ** self.second) + self.third
        print("MoreFourcal pos : {0}".format(result))
    def show_child(self):
        super().show_parent()
        print("자식클래스")
import mod
a = mod.MoreFourcal(4, 2, 5)
a.add()
a.pos()
a.div()
a.show_child()
Fourcal 생성자
MoreFourcal 생성자
Fourcal add : 6
MoreFourcal pos : 21
Fourcal div : 2.0
부모클래스
자식클래스

메소드 오버라이딩


class Fourcal():
    def __init__(self, first, second):
        self.first = first
        self.second = second
        print("Fourcal 생성자")
    def add(self):
        result = self.first + self.second
        print("Fourcal add : {0}".format(result))
    def mul(self):
        result = self.first * self.second
        print("Fourcal mul : {0}".format(result))
    def sub(self):
        result = self.first - self.second
        print("Fourcal sub : {0}".format(result))
    def div(self):
        result = self.first / self.second
        print("Fourcal div : {0}".format(result))
    def show():
        print("부모클래스")

class MoreFourcal(Fourcal):
    def __init__(self, first, second, third):
        super().__init__(first, second)
        self.third = third
        print("MoreFourcal 생성자")
    def pos(self):
        result = (self.first ** self.second) + self.third
        print("MoreFourcal pos : {0}".format(result))
    def div(self):
        result = self.first % self.second
        print("MoreFourcal div : {0}".format(result))
import mod
a = mod.MoreFourcal(1,2,3)
a.div()
Fourcal 생성자
MoreFourcal 생성자
MoreFourcal div : 1
반응형

'Python' 카테고리의 다른 글

[Python] dir() 함수  (60) 2024.04.04
[Python] enumerate() 함수  (61) 2024.03.28
[Python] print() 함수  (60) 2024.03.27
[Python] input() 함수  (59) 2024.03.26
[Python] zip() 함수  (66) 2024.03.22

댓글