Python04 :: python class
ㅡ. class 의 정의
추상화를 구체화합니다.
ㅡ. class 정의하기
(구조)
class ClassName <statement-1> 내용 <statement-n> e.g) class Cat: def meow(self): print("야용") cat1 = Cat() //객체 생성 cat1.meow()
ㅡ. 인스턴스 변수 생성
class Cat: def info(self) : self.name = "돼지" self.color ="pink" print("컴퓨터 이름은", self.name, "색깔은", self.color) cat = Cat() #인스턴스 생성 cat.info() #인스턴스 메소드 실행
ㅡ. self
클래스의 인스턴스를 지칭한다.
self 를 통해 메소드와 속성을 접근한다.
모든 메소드의 첫 번째 매개변수는 자기 자신을 가리키는 self 가 존재한다.
class Cat: def __init__(self, name="나비", color="흰색"): #초기화 메소드 self.name = name self.color = color def info(self) print("돼지 이름은", self.name, "색깔은", self.color) cat1 = Cat("네로", "검정색") cat2 = Cat("미미", "갈색") cat1.info() cat2.info()
self는 클래스의 인스턴스를 지칭합니다.
'Python' 카테고리의 다른 글
Python07 :: python 클래스 이름, 클래스 변수 (0) | 2020.08.14 |
---|---|
Python06 :: Python Project 구성하기 (0) | 2020.08.14 |
Python05 :: python nested dictionary (0) | 2020.08.13 |
Python03 :: Class (0) | 2018.12.30 |
Python02 :: sort (0) | 2018.12.29 |
댓글
이 글 공유하기
다른 글
-
Python06 :: Python Project 구성하기
Python06 :: Python Project 구성하기
2020.08.14 -
Python05 :: python nested dictionary
Python05 :: python nested dictionary
2020.08.13 -
Python03 :: Class
Python03 :: Class
2018.12.30 -
Python02 :: sort
Python02 :: sort
2018.12.29
댓글을 사용할 수 없습니다.