1 분 소요

3.super

  • 하위클래스(자식클래스)에서 부모 클래스의 method를 호출할때사용

person class를상속 받아 student class를 만듬

학생의 속성으로 이름, 나이, 학번(stu_num) 학생 객체를 만들어서 학번을 출력


class Student(Person):
    def __init__(self, name, age, stu_num):
        super().__init__(name, age) # 부모클래스에 있는 호출함 (self 쓰지 않음)
        self.stu_num = stu_num

student1 = Student('김길동',21, 202022)
student1.stu_num 
    202022

 

student가 아르바이트를 하는경우 일도 하고 공부도 하는 studnet 객체를 만드세요

work() 호출시 super( ) 사용

홍길동은 30분동안 일을 합니다 그리고 30분동안 공부도 합니다 그리고 30분동안 공부도 합니다

 
class Student(Person):
    def work(self,minute):
        super().work(minute)
        print(f'그리고 {minute}동안 일을 합니다 그리고 {minute}동안 공부 합니다')
        
hong = Student('홍길동','23')
hong.work(30)
 
    홍길동는 30분동안 일합니다..
    그리고 30동안 일을 합니다 그리고 30동안 공부 합니다

 

phone 객체를 만들고

phone 객체를 상속받는 smartphone 객체를 만드세요

phone 객체의 속성

marker,price,color

메소드 phone_info: 제조사,컬러,가격을 출력

class Phone:
    def __init__(self, marker, price, color):
        self.marker = marker
        self.price = price
        self.color = color
        
    def phone_info(self): 
        print('제조사:', self.marker)
        print('가격:', self.price)
        print('컬러:', self.color)

class smartphone(Phone):
    pass
        

 

iphone = smartphone('apple', '164', 'white')
iphone.phone_info()
print(iphone.marker) #marker 만 출력하고 싶을때
    제조사: apple
    가격: 164
    컬러: white
    apple

 

class Phone:
    def __init__(self, marker, details):
        self.marker = marker
        self.details = details
    

    def phone_info(self):
         print(f'제조사:{self.marker},제품상세:{self.marker}')

class smartphone(Phone):
    pass
        
iphone = smartphone('iphone', {'price':1000,'color':'white'})
iphone.phone_info()

#컬러만 출력
print(iphone.details['color'])

#가격만 출력
print(iphone.details['price'])

#제조사만 출력
print(iphone.marker)
               

    제조사:iphone,제품상세:iphone
    white
    1000
    iphone

카테고리:

업데이트:

댓글남기기