Skip to content

Latest commit

 

History

History
141 lines (114 loc) · 3.56 KB

07-1 類別-基本使用.md

File metadata and controls

141 lines (114 loc) · 3.56 KB

07-1 類別(class)-基本使用

用class敘述建立一個類別, 它可以生成多個這個類別的實例(instance). 在類別中的__init__()函式是它的建構元, 其內的self表示在記憶體中生成的那個實例.

(1)程式範例

ntub.py
#------------------------------------------------
# 定義學生類別
#------------------------------------------------
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 '日間部'            
main.py
#--------------------------------------------
# 匯入ntub模組
#--------------------------------------------
import ntub

#--------------------------------------------
# 生成實例s
#--------------------------------------------
s=ntub.Student('1056001', '王小明', '6')

#--------------------------------------------
# 呼叫s的方法
#-------------------------------------------- 
print('學號:{}  姓名:{}  系別:{}  學制:{}'.format(s.stuNo, s.stuName, s.dept(), s.division()))

執行結果:

學號:10556001  姓名:王小明  系別:資管系  學制:日間部

(2)程式範例

ntub.py
#------------------------------------------------
# 定義學生類別
#------------------------------------------------
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 '日間部'            
main.py
#---------------------------
# 匯入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  姓名:李小強  系別:其他系  學制:日間部