728x90
반응형

지난포스트 (deprecated)/Python (deprecated) 3

python - __init__ 과 __str__ 메소드 사용

class Human(): def __init__(self, name, weight): '''초기화 함수''' print("__init__실행") self.name = name self.weight = weight def __str__(self): '''문자열화 함수''' return "{} (몸무게 {}kg)".format(self.name, self.weight) def eat(self): self.weight += 0.1 print("{}가 먹어서 {}kg이 되었습니다." .format(self.name, self.weight)) def walk(self): self.weight -= 0.1 print("{}가 걸어서 {}kg이 되었습니다." .format(self.name, self.weight)..

python - 메소드 개념

조건 '철수' 의 몸무게는 '60.5'다 먹으면 0.1kg이 찌고 걸으면 0.1kg이 빠진다. 1. 클래스 Human 생성 2. create, eat, walk 함수 class 내부에 생성 3. create 에는 person 이라는 인스턴스를 생성해주고 매개변수를 받아 person 을 다시 리턴해준다. 4. eat 에는 몸무게가 증가하는 함수 작성 5. walk 에는 몸무게가 감소하는 함수 작성 class Human(): def create(name, weight): person = Human() person.name = name person.weight = weight return person def eat(self): self.weight += 0.1 print("{}가 먹어서 {}kg이 되었습니다...

728x90
반응형