본문 바로가기
코리아 IT아카데미/python

8일차 수업 | class

by Sharon kim 2021. 10. 4.

StudentClass.py

class Student:
    name = ''
    studentYear = 0
    studentGroup = 0
    studentNumber= 0

    def setStudent_info(self, name, sYear, sGroup, sNum):
        self.name = name
        self.studentYear = sYear
        self.studentGroup = sGroup
        self.studentNumber = sNum

    def showStudent_info(self):
        print('<학생정보>')
        print(f'이름:{self.name}')
        print(f'학년:{self.studentYear}')
        print(f'반:{self.studentGroup}')
        print(f'번호:{self.studentNumber}')

ClassEx.py

#p.258
import StudentClass

# name1 = '김영영'
# studentYear1=2 #학년
# studentGroup1=1 #반
# studentNumber1=3 #번호
#
# name2='김영일'
# studentYear2=3
# studentGroup2=2
# studentNumber2=1
#
# name = ['김가영','김가일','김가이']

s1 = StudentClass.Student()
s2 = StudentClass.Student()
s3 = StudentClass.Student()
s4 = StudentClass.Student()
s5 = StudentClass.Student()
s6 = StudentClass.Student()
s7 = StudentClass.Student()
s8 = StudentClass.Student()

s1.setStudent_info('김영영',3,1,2)
s2.setStudent_info('김영일',2,2,1)
s3.setStudent_info('김영이',1,3,4)
s4.setStudent_info('김영삼',1,2,4)
s5.setStudent_info('김영사',2,3,2)
s6.setStudent_info('김영오',1,3,3)
s7.setStudent_info('김영육',2,2,4)
s8.setStudent_info('김영칠',3,1,5)

s = [s1,s2,s3,s4,s5,s6,s7,s8]
for student in s:
    student.showStudent_info()

# s1.showStudent_info()
# s2.showStudent_info()
# s3.showStudent_info()
# s4.showStudent_info()
# s5.showStudent_info()
# s6.showStudent_info()
# s7.showStudent_info()
# s8.showStudent_info()

# print(s1)
# print(s2)

practice15-1.py

#책제목(title), 저자(author)
#(0)
class Book:
    title : ''
    author : ''

    #(1)
    def set_info(self, title, author):
        self.title = title
        self.author = author

    #(4)
    def print_info(self):
        print(f'책 제목:{self.title}')
        print(f'책 저자:{self.author}')
#(2)
book1 = Book()
book2 = Book()

#(3)
book1.set_info('파이썬','민경태')
book2.set_info('어린왕자','생텍쥐베리')

# print(book1.title)
# print(book1.author)

#(5)
# book1.print_info()
# book2.print_info()
#(5-1)
b = [book1,book2]
for book in b:
    book.print_info()

CarClass.py

class Car:
    company = ''
    serialNumber = ''
    model = ''
    color = ''
    #company(제조사),serialNumber(일련번호),model(모델명),color(색상)

    # set_car_info()
    def set_Car_info(self,company,serialNumber,model,color):
        self.company = company
        self.serialNumber = serialNumber
        self.model = model
        self. color = color
    # show_car_info()
    def show_Car_info(self):                  #문자열 곱하기
        print('=====<차량정보>=====') # print(f'{"="*5}<차량정보>{"="*5}')
        print(f'제조사:{self.company}')
        print(f'일련번호:{self.serialNumber}')
        print(f'모델명:{self.model}')
        print(f'색상:{self.color}')
        print(f"{'=' * 19}")
        # <차량정보>
        # 제조사:
        # 일련번호:
        # 모델명:
        # 색상:

# 인스턴스 tesla1(테슬라, t20210001, model3, 화이트), tasla2(테슬라, t20210002, modelS, 블랙)
# kia1(기아자동차, k20210001, k8, 블루)
# hyundai1(현대자동차, h20210001, 그랜저, 그레이)

tasla1 = Car()
tasla2 = Car()
kia1 = Car()
hyundai1 = Car()

tasla1.set_Car_info('테슬라', 't20210001', 'model3', '화이트')
tasla2.set_Car_info('테슬라', 't20210002', 'modelS', '블랙')
kia1.set_Car_info('기아자동차', 'k20210001', 'k8', '블루')
hyundai1.set_Car_info('현대자동차', 'h20210001', '그랜저', '그레이')

tasla1.show_Car_info()
tasla2.show_Car_info()
kia1.show_Car_info()
hyundai1.show_Car_info()

practice15-3.py

class Song:
    title = ''
    genre = ''

    # def __init__(self):
    #     self.title = title
    #     self.genre = genre

    def set_song(self, title, genre):
        self.title = title
        self.genre = genre

    def print_song(self):
        print(f'노래제목:{self.title}({self.genre})')

    # def __del__(self):
    #

class Singer:
    singerName = ''

    def set_singer(self, singName):
        self.singerName = singName

    def hit_song(self, song):
        self.song = song
        #song - > 객체라서 초기화하기 애매해서 변수없음

    def print_singer(self):
        print(f'가수이름:{self.singerName}')
        song.print_song()

#song 인스턴스 생성
# song = Song('취중진담','발라드')
song = Song()
song.set_song('취중진담','발라드')

# print(type(song))

#singer 인스턴스 생성
singer = Singer()
singer.set_singer('김동률')

#singer의 대표곡 지정
singer.hit_song(song)

#singer 정보출력
singer.print_singer()
# del song