티스토리 뷰

Python

클래스 만들기

Yokina 2017. 1. 7. 23:20

Class


class 클래스이름:

변수나 함수

class bread:

meterial = "팥"

def say(self):

print("%s 빵" %self.meterial)

first_bread = bread()

first_bread.say(


  • self 는 인스턴스 자신  타언어의 this와 같은것

  • 위의 경우 클래스 내 함수에서 self를 제거하면 에러가 발생함.







생성자

class bread:                                                                              

def __init__(self.m):

self.meterial = m

def say(self):

print("I like %s빵" %self.meterial)

__init__ (_기호 연속으로 2번)



상속

class human:

name = "홍길동"

def intro(self):

print("My name is %s" %self.name)


class child(human):                          #human 를 상속받아 child를 만든다

def hello(self):

print("I am son of %s" %self.name)


jinsu = child() #child 클래스를 인스턴스로 생성합니다.

jinsu.hello() #jinsu 인스턴스 hello() 사용, 이름은 부모변수사용


  • 다중 상속도 가능함

calss father:

s_name = "홍길동"

def intro(self):

print("나는 %s 부모입니다" %self.s_name)


class tail:

height - 180

def hgt(self):

print("키는 %s cm입니다" %self.height)


class child(father, tail):

name = "일지매"

def hello(self):

print("안녕 %s입니다" %self.name)





'Python' 카테고리의 다른 글

모듈 소개  (0) 2017.01.07
모듈 Module  (0) 2017.01.07
함수 만들기  (0) 2017.01.07
반복문  (0) 2017.01.07
List & Collection  (0) 2017.01.07
댓글
공지사항
최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday
링크
TAG
more
«   2025/05   »
1 2 3
4 5 6 7 8 9 10
11 12 13 14 15 16 17
18 19 20 21 22 23 24
25 26 27 28 29 30 31
글 보관함