- 똑같은 무언가를 만들어내기 위한 설계도(틀) -> 쿠키 커터(틀)
- 그럼 이 설계도로 만들어낸 것은? 객체! -> 쿠키 커터(틀)로 찍어낸 쿠키
def calc_bmi(height, weight):
# 키는 입력받을 때 cm 단위로 입력받지만, 계산을 위해 m 단위로 변경해줌
height = height * 0.01
return weight / (height ** 2)
person_1_name = "Jack"
person_1_height = 170
person_1_weight = 80
person_2_name = "John"
person_2_height = 180
person_2_weight = 80
person_1_bmi = calc_bmi(person_1_height, person_1_weight)
person_2_bmi = calc_bmi(person_2_height, person_2_weight)
print("{}'s bmi = {:.2f}".format(person_1_name, person_1_bmi))
print("{}'s bmi = {:.2f}".format(person_2_name, person_2_bmi))
class Person:
def __init__(self, name, height, weight):
self.name = name
self.height = height
self.weight = weight
def bmi(self):
# 키는 입력받을 때 cm 단위로 입력받지만, 계산을 위해 m 단위로 변경해줌
height = self.height * 0.01
return self.weight / (height ** 2)
def print_bmi(self):
print("{}'s bmi = {:.2f}".format(self.name, self.bmi()))
person_1 = Person("Jack", 170, 80)
person_2 = Person("John", 180, 80)
person_1.print_bmi()
person_2.print_bmi()
class Person:
def __init__(self, name, age, job): # 클래스 안에 구현된 함수 == 매서드(Method)
self.name = name # self.name으로 재정의하는 이유 -> 클래스 값을 들고 있는 새로운 변수를
self.age = age
self.job = job
def print_info(self):
print(f"저의 이름은 {self.name}입니다.") # f를 안쓰면 self.name이 그대로 출력된다
print(f"전 {self.age}살이구요. 직업은 {self.job}입니다.")
p1 = Person("Joy", 27, "Programmer")
p1
p1.print_info()
class BibimBob:
def __init__(self, rice, j1, j2, j3):
self.rice = rice
self.j1 = j1
self.j2 = j2
self.j3 = j3
def print_all_j(self):
print(f"밥은 {self.rice}!")
print(f"재료1은 {self.j1}!")
print(f"재료2는 {self.j2}!")
print(f"재료3은 {self.j3}!")
def mix(self):
print(f"{self.rice}에 {self.j1}, {self.j2}, {self.j3}를 섞어 만든 비빔밥이 완성되었습니다!")
bibimbob = BibimBob("쌀밥", "참기름", "고추장", "계란후라이")
bibimbob
bibimbob.mix()
class Multiple: # class 무조건 첫 글자 대문자!
def __init__(self, num1, num2):
self.num1 = num1
self.num2 = num2
def mul(self):
return self.num1 * self.num2
def print_mul(self):
print(f"{self.mul()}은 {self.num1}과(와) {self.num2}를 곱한 값입니다!")
m = Multiple(15, 2)
m.print_mul()
# 1. 새로운 노트북 생성
# 2. 함수 작성
def calc_bmi(height, weight):
height = height * 0.01
return weight / (height ** 2)
# 3. .py 파일로 다운
# 4. 사용 중인 노트북과 동일한 위쳉 저장 후 파일명 bmi.py로 변경
# 모듈 사용방식 1 : import 모듈이름
import bmi as b1
# 모듈 사용방식 2 : from 모듈이름 import 모듈 안의 함수
from bmi import calc_bmi as b2
print(bmi)
print(bmi.calc_bmi(180,80))
print(calc_bmi(180,80))