정리노트

[파이썬/python] [클래스] 객체생성, 기본원리 연습-정리 본문

프로그래밍/Python

[파이썬/python] [클래스] 객체생성, 기본원리 연습-정리

Rolen 2022. 9. 25. 00:46
class Point:
    # 인스턴스 변수선언, 매개변수 = 디폴트값 초기화
    def __init__(self, math = 0, science = 0):
        self.math = math
        self.science = science


    # set 완료 후 데이터지정된 객체별 출력위한 함수
    def show(self):
        print(f"수리 : {self.math}, 과학 : {self.science}")


    # 수리 점수 set // 정리만 잘해둔다면 다른 과목과 한 함수에 한 번에 해도됨. //
    def setMath(self, math):
        self.math = math
        
    # 과학 점수 set
    def setScience(self, science):
        self.science = science


    # 객체 선언
a = Point()
b = Point()
c = Point()

    # class에 만들어둔 함수로 각 객체 값 할당
a.setMath(30)
a.setScience(100)
b.setMath(100)
b.setScience(80)
c.setMath(50)
c.setScience(70)

a.show() // 수리 : 30, 과학 : 100
b.show() // 수리 : 100, 과학 : 80
c.show() // 수리 : 50, 과학 : 70
728x90