#------------------------------------------------
# 定義學生類別
#------------------------------------------------
class Student():
#-------------------
# 建構元
#-------------------
def __init__(self, stuNo, stuName, deptNo):
self.stuNo=stuNo
self.stuName=stuName
self.deptNo=deptNo
#-------------------
# 取出系名的方法
#-------------------
def dept(self):
if self.deptNo=='6':
return '資管系'
else:
return '其他系'
#-------------------
# 取出學制的方法
#-------------------
def division(self):
if self.stuNo[0]=='N':
return '進修推廣部'
else:
return '日間部'
#--------------------------------------------
# 匯入ntub模組
#--------------------------------------------
import ntub
#--------------------------------------------
# 生成實例s
#--------------------------------------------
s=ntub.Student('1056001', '王小明', '6')
#--------------------------------------------
# 呼叫s的方法
#--------------------------------------------
print('學號:{} 姓名:{} 系別:{} 學制:{}'.format(s.stuNo, s.stuName, s.dept(), s.division()))
執行結果:
學號:10556001 姓名:王小明 系別:資管系 學制:日間部
#------------------------------------------------
# 定義學生類別
#------------------------------------------------
class Student():
#-------------------
# 建構元
#-------------------
def __init__(self, stuNo, stuName, deptNo):
self.stuNo=stuNo
self.stuName=stuName
self.deptNo=deptNo
#-------------------
# 取出系名的方法
#-------------------
def dept(self):
if self.deptNo=='6':
return '資管系'
else:
return '其他系'
#-------------------
# 取出學制的方法
#-------------------
def division(self):
if self.stuNo[0]=='N':
return '進修推廣部'
else:
return '日間部'
#---------------------------
# 匯入ntub模組
#---------------------------
import ntub
#---------------------------
# 將建成物件的原始資料
#---------------------------
data=[('10556001', '王小明', '6'), ('10556002', '陳小華', '6'), ('10551003', '李小強', '1')]
#---------------------------
# 將放置多個物件的list
#---------------------------
students=[]
#-----------------------------------------
# 生成Student類別的實例, 將它加入list中
#-----------------------------------------
for d in data:
students.append(ntub.Student(d[0], d[1], d[2]))
#---------------------------
# 逐一呼叫各個物件的方法
#---------------------------
for s in students:
print('學號:{} 姓名:{} 系別:{} 學制:{}'.format(s.stuNo, s.stuName, s.dept(), s.division()))
執行結果:
學號:10556001 姓名:王小明 系別:資管系 學制:日間部
學號:10556002 姓名:陳小華 系別:資管系 學制:日間部
學號:10551003 姓名:李小強 系別:其他系 學制:日間部