0414 파이썬 중급 4일차(~25)

박영선·2023년 4월 14일
0

객체 복사에 대한 이해

얕은복사 : 객체 주소를 복사 (객체 자체가 복사되는것이 아님)

깊은복사 : 객체 자체를 복사, 또 하나의 객체가 만들어진다

얕은복사

tc2는 tc1의 객체 주소를 복사 / tc2 정보를 바꾸면 tc1=tc2이므로 1도 바뀜

깊은복사

copy 모듈을 사용하면 tc2 객체 자체를 따로 복사할 수 있다. 이렇게 복사하면 tc2는 별개의 객체가 되므로 정보를 바꿔도 tc1에는 영향이 없다

클래스 상속


클래스 괄호 사이에 상속대상 클래스 명을 넣어준다.

생성자

객체 생성 시 생성자를 호출하고 그 생성자가 init 메서드를 호출해서 객체가 생성된다.

init은 속성을 초기화

생성자 호출 시 값을 넣어주면(10,20) 이 값이 init으로 들어감(n1,n2) 이게 num1과 num2를 초기화

상위클래스 속성 초기화는 super() 사용

실습

class Midexam :

    def __init__(self,s1,s2,s3):
        print('[MidExam]__init__()')

        self.mid_kor_score = s1
        self.mid_eng_score = s2
        self.mid_mat_score = s3

    def printScores(self):
        print(f'mid_kor_score : {self.mid_kor_score}')
        print(f'mid_eng_score : {self.mid_eng_score}')
        print(f'mid_mat_score : {self.mid_mat_score}')

class Endexam(Midexam):

    def __init__(self, s1, s2, s3, s4, s5, s6):
        print('[Exam]__init__()')

        super().__init__(s1,s2,s3)

        self.end_kor_score = s4
        self.end_eng_score = s5
        self.end_mat_score = s6

    def printScores(self):
        super().printScores() #중간고사 출력
        print(f'end_kor_score : {self.end_kor_score}')
        print(f'end_eng_score : {self.end_eng_score}')
        print(f'end_mat_score : {self.end_mat_score}')

    def getTotalScore(self):
        total = self.mid_kor_score + self.mid_eng_score + self.mid_mat_score
        total += self.end_kor_score + self.end_mat_score + self.end_eng_score

        return total

    def getAverageScore(self):
        return self.getTotalScore()/6

exam = Endexam(85,90,96,88,75,60)
exam.printScores()

print(f'Total: {exam.getTotalScore()}')
print(f'Average: {round(exam.getAverageScore(),2)}')

다중상속

2개 이상의 클래스를 상속

다중상속 너무 많이 하면 안됨. 중복된 코드가 많으면 헷갈린다.

profile
데이터분석 공부 시작했습니다

0개의 댓글