Bu̍t-kiāⁿ tō-hiòng thêng-sek sè-kè

Wikipedia (chū-iû ê pek-kho-choân-su) beh kā lí kóng...
跳至導覽 跳至搜尋

Bu̍t-kiāⁿ tō-hiòng têng-sek sè-kè iah-sī bīn-hiòng tùi-siōng pian-têng (Eng-gí: Object-oriented programming, OOP), tiō sī kheng-hiòng iōng bu̍t-kiāⁿ (mi̍h-kiaⁿ, tùi-siōng)--ê khài-liām lâi siá--ê têng-sek sè-kè-hoat (programming paradigm). Iōng che, lán-lâng thang kā ta̍k khoán tāi-chì iah-sī bu̍t-phín iōng "bu̍t-kiāⁿ"--ê kài-liām lâi bô͘-gí (thiu-siōng-hòa, abstraction). Bu̍t-kiāⁿ ū i kā-ki--ê sio̍k-sèng (attribution) piàn-sò͘, mā thang iōng i ka-kī--ê hong-hoat (method, chi̍t-chióng têng-sū), chiap-siu kî-tha--ê bu̍t-kiāⁿ, kap hőng thoân kap kî-tha--ê hâm-sò͘ iah-sī hong-hoat.

Lūi-pia̍t, sio̍k-sèng kap hong-hoat[siu-kái | kái goân-sú-bé]

Bu̍t-kiaⁿ thang hun-chò kui--ê lūi-pia̍t (class), chi̍t-ê lūi-pia̍t ū i--ê sio̍k-sèng kap hong-hoat. Lē (iōng Python 3):

# tēng-gī lūi-pia̍t "Káu"
class Káu:
    def __init__(self, name, length):
        '''Kiàn-li̍p "Káu" lūi-hêng lāi-té--ê bu̍t-kiāⁿ--ê sî pháng--ê hong-hoat'''
        '''Ē-kha sī sio̍k-sèng'''
        self.name = name #miâ
        self.length = length
    
    def pùi(self, string):
        '''"pùi" sī chi̍t-khoán hong-hoat'''
        print("%s, áu!" % string)

lâi_hù = Káu("Lâi-hù", 80) # tēng-gī lâi_hù sī káu

# ìn lâi_hù--ê sio̍k-sèng (miâ, ta̍t sī "Lâi-hù")
print(lâi_hù.name)

# sú-iōng hong-hoat "pùi", ìn-chhut "Góa sī chi̍t chiah káu, áu!"
lâi_hù.pùi("Góa sī chi̍t chiah káu") 

Kè-sêng (Inheritance)[siu-kái | kái goân-sú-bé]

Chi̍t-ê lūi thang sī pa̍t--ê lūi--ê "kiáⁿ-lūi", kè-sêng goân-iú--ê (pō͘-hūn) sio̍k-sèng kap hong-hoat, ē-tàng hō͘ kiáⁿ-lūi lâi sú-iōng.

To-thài (Polymorphism)[siu-kái | kái goân-sú-bé]

Bô-kâng--ê lūi-pia̍t thang ū kâng-khoán--ê hong-hoat, tio̍h sī khoán to-thàì (多態)--ê lē.

Chi-goân ê têng-sek gí-giân ê lē[siu-kái | kái goân-sú-bé]