# coding:utf-8
class Student:
name = u"철수"
no = 0
address = ""
def __init__(self,a,b,c):
self.name = a
self.no = b
self.address = c
def __del__ (self):
print self.name+u"죽는다"
def run (self):
print self.name+u"가 달립니다!"
def study (self):
print self.name+u"가 공부합니다!"
def play (self, a):
print self.name+u"가 "+a+u"랑 놉니다"
a = Student(u"미라",3000, u"부대동")
a.no = 1000
a.address = u"봉천동"
print a.name
b = Student(u"미라",10000,u"천안")
b.name = u"영희"
b.no = 20000
b.address = u"가리봉동"
print b.name
c = Student(u"미라",3000, u"부대동")
sList = [a,b,c]
for x in sList :
print x.name
print x.no
print x.address
x.play(u"영애")
x.run()
del c
print u"끝이에요"
class CStudent(Student):
CAddress = ""
def __init__ (self, a,name):
self.CAddress = a
self.name = name
d = CStudent(u"난징","등소평")
d.name = u"주은래"
d.run()
sList[2].run()
사람이라는 수업에서한 클래스에 세개의 멤버변수와 두개의 메소드를 추가합니다. 물론 이 다섯개 추가된 기능을 확인하는 부분이 있어야겠지요. 기존에 수업에서 만들었던 멤버변수와 메소드는 그대로 사용하고 여기에 추가하는 겁니다.
풀이
class person: #멤버변수 5개 ↓
name = "" #멤버변수 이름
age = 0 #멤버변수 나이
num = 0 #멤버변수 학번
phone = "" #멤버변수 폰번호
birth = 0 #멤버변수 생일
def __init__(self,a,b,c,d,e):
self.name = a
self.age = b
self.num = c
self.phone = d
self.birth = e
def introduceMe(self): # 메소드 추가 5개 ↓
print u"저의 이름은 %s + 나이는 %d살 입니다." %(self.name, self.age)
def callMe(self,m):
print u"%s야 %d로 전화 걸어줘" %(m,self.phone)
def tellMyBirthday(self):
print u"저는 %s 에 테어났습니다" %self.birth
def love(self,you):
print u"그리고 %s은 %s를 완전 사랑합니다.ㅋㅋㅋㅋㅋㅋ"%(self.name,you)
wonho = person(u"허원", 25, 7969314,01011111111,12.17)
wonho.introduceMe()
wonho.callMe(u"영희")
wonho.tellMyBirthday()
wonho.love(u"영희")
풀이
class Person: #멤버변수 5개 ↓
name = "" #멤버변수 이름
age = 0 #멤버변수 나이
num = 0 #멤버변수 학번
phone = "" #멤버변수 폰번호
birth = 0 #멤버변수 생일
def __init__(self,a,b,c,d,e):
self.name = a
self.age = b
self.num = c
self.phone = d
self.birth = e
def introduceMe(self): # 메소드 추가 5개 ↓
print u"저의 이름은 %s 이고 나이는 %d살 입니다." %(self.name, self.age)
def callMe(self,m):
print u"%s야 %d로 전화 걸어줘" %(m,self.phone)
def tellMyBirthday(self):
print u"저는 %s 에 테어났습니다" %self.birth
def love(self,you):
print u"그리고 %s은 %s를 완전 사랑합니다.ㅋㅋㅋㅋㅋㅋ"%(self.name,you)
class Student(Person): # 이전에 만든 고유한 클래스를 상속받음
score = "" # 멤버변수 추가(좋아하는 것)
school = "" # 멤버변수 추가(싫어하는 것)
def __init__(self,a,b,c,d,e,f,g):
self.name = a
self.age = b
self.num = c
self.phone = d
self.birth= e #추가 변수
self.score = f #추가 변수
self.school = g
def studying(self): # 메소드 추가
print u"%s 가 열심히 공부하고 %d 점을 받았습니다" %(self.name,self.score)
wonho = Student(u"허원", 23, 7969314, 0101231122, u"2000년 1월 1일", 100, u"공주대학교") # 상속한 클래스로 객체 만듬
wonho.introduceMe()
wonho.studying()