티스토리 뷰

프로그램 이야기/Python 이야기

Python Class 상속

야생의개발자 2008. 12. 11. 17:37

spi = ["NONE","Women","Wolf","Moster"]

class human:
    def __init__(self):
        self.name = "NONE"
        self.age = 0
        self.Species = "NONE"

    def insert(self):
        print "Input Normal method"
        self.name = raw_input("NAME : ")
        self.age = int(raw_input("AGE : "))
        tmp = int(raw_input("1. Women, 2. Wolf, 3. Moster : "))
        if tmp < 1 or tmp > 3:
            tmp = 0;
        self.Species = spi[tmp]

    def __str__(self):
        return "NAME : %s, AGE : %d, SPECIES : %s"\
              %(self.name,self.age,self.Species)


        
class teacher(human):
    def __init__(self):
        human.__init__(self)
        self.subjects = []

    def insert(self):
        human.insert(self)
        self.subjects.append(raw_input("Subject : "))

    def __str__(self):
        return "%s\nSUBJECTS : %s"%(human.__str__(self),self.subjects)

    def insertsub(self):
        self.subjects.append(raw_input("Subject : "))

글자가 너무 긴 나머지, 자동으로 축소 기능으로 인해, 조금 작아 졌군...

상속에서는 상속 받은 클래스에서 부모 클래스의 기능을 저런 식으로 손쉽게 사용 가능하다.

human.__init__(self)

추가 되는 것만 따로 구현하면 상속은 그냥 끝
공지사항
최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday
«   2024/03   »
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
글 보관함