close
import os
import sys
import copy
from functools import reduce

class Score():
    def __init__(self, score):
        self.__score = score

    def getscore(self):
        print("inside the getscore")
        return self.__score

    def setscore(self,score):
        print("inside the setscore")
        self.__score = score

def main():
    stu = Score(0)
    print(stu.getscore())
    stu.setscore(80)
    print(stu.getscore())

if (__name__=='__main__'):
    main()

 

 

import os
import sys
import copy
from functools import reduce

class Score():
    def __init__(self, score):
        self.__score = score

    def getscore(self):
        print("inside the getscore")
        return self.__score

    def setscore(self,score):
        print("inside the setscore")
        self.__score = score


    '''
    新式的Python 會使用property()的方法
    
        新式屬性 = property(getter[,setter[,fdel[,doc]]])
            - getter : 獲取屬性值函數
            - setter : 設定屬性值函數
            - fdel   : 刪除屬性值函數
            - doc    : 屬性描述
        然後return 的是新式屬性,未來就可以使用這個來存取了 !
    '''
    sc = property(getscore,setscore)

def main():
    stu = Score(0) #step1: init, set score = 0
    print(stu.sc) # output : 0
    stu.sc = 80
    print(stu.sc) # output : 80

if (__name__=='__main__'):
    main()

 

 

arrow
arrow
    全站熱搜

    Eric 發表在 痞客邦 留言(0) 人氣()