KG_KAIROS/Python

[KG_KAIROS] 파이썬 강의_2 (Class)

projectlim 2024. 7. 1. 17:23
728x90
반응형
SMALL

2024.07.01

 

파이썬 강의로 오늘은 Class에 대해 학습하였습니다.

간단한 예제를 통해 class 생성과 class를 사용해보았는데요.

class giftset():
  def __init__(self,일반,야채,고추):
    self.일반=일반
    self.야채=야채
    self.고추=고추

  def output(self,setname):
    print(setname)
    print('일반',self.일반)
    print('야채',self.야채)
    print('고추',self.고추)


gs=giftset(12,3,3)
gs.output('gift')
gs1= giftset(20,5,10)
gs1.output('gift1')

결과창

 

다음 예제는 강아지 class

class Dog:
  species = "Canis familiars"

  def __init__(self,name,age,breed):
    self.name=name
    self.age=age
    self.breed=breed

  def __str__(self):
    return f"{self.name} is {self.age} years old"

  def speak(self, sound):
    return f"{self.name} says {sound}"


kara = Dog("kara", 4,"samoyed")
woods = Dog("wood", 5,"골댕이")
jim = Dog("jim", 5,"bulldog")

print(kara)
print(kara.speak("yap"))

결과창

 

그리고 상속과 모듈을 활용한 개인 프로젝트로 간단한 실습을 진행하였습니다.

저는 전 시간에 했던 학점 생성기를 응용해서 모듈을 제작하였습니다.

 

Score라는 클래스를 상속받아서 output이라는 클래스를 제작하였습니다.

 

op로 선언해주고 함수를 불러와서 실행시켰습니다.

 

 

class Score():
    def __init__(self,score):
        self.score = score

    def ABCDEF(self):
        self.score =int(input("학점입력 : "))
    
    def result_score(self):
        if self.score>=90 :
            print("A학점")
        elif self.score>=80  :
            print("B학점")
        elif self.score>=70  :
            print("C학점")
        elif self.score>=60  :
            print("D학점")
        else:
            print("재수강 가즈아")

 

import TermProject.factory
class output(TermProject.factory.Score):
    def __init__(self, score):
        super().__init__(score)

score = 1

op= output(score)
op.ABCDEF()
op.result_score()

경로 위치 확인

factory파일이 메인입니다. 

TermProject폴더에 생성을 했고 module 파일은 TermProject 파일 밖에 제작하였습니다.

 

실행화면

 

실행시키면 정상작동하는걸 확인할 수 있습니다. 

728x90
반응형
LIST