[Python 기초] Class 만들기
Class
클래스 생성
class 클래스이름:
으로 생성
1
2
class Aircon:
# Aircon 클래스를 생성
변수 선언
1
2
3
class Aircon:
temp = 20
wind_speed = 1
메서드 선언
파이썬에서 클래스의 메서드에는 첫 번째 매개변수를 전달해주어야 한다.
매개변수명이 반드시 self
일 필요는 없지만 컨벤션상 주로 self
를 쓴다.
self
는 자바스크립트에서의 this
와 같은 의미이며, 메서드가 호출될 때 self
자리에 객체 자신을 인자에 넣는다.
1
2
3
class Aircon:
def wind_speed_up(self):
self.wind_speed = self.wind_speed + 1
인스턴스 생성
instance = ClassName()
형식으로 생성한다.
1
2
crystal = Aircon()
#Aircon의 성질을 가진 객체 crystal 생성
클래스 상속
1
2
3
4
5
6
7
class Tree:
height = 0
leaf_size = 0
class CherryBlossom(Tree): # Tree의 성질을 그대로 계승!
height = 25 # 인스턴스 값 변경 가능
leaf_size = 1
This post is licensed under CC BY 4.0 by the author.