# -*- coding: utf-8 -*- class Student: def __init__(self, mand, math, soc, nat, eng, home_income, name = "小明"): self.name = name self.mand = mand self.math = math self.soc = soc self.nat = nat self.eng = eng self.__home_income = home_income def sum(self): """ 計算各科總分 """ return self.mand + self.math + self.soc + self.nat + self.eng def ave(self): """計算個人平均分數""" return self.sum()/5.0 def datainfo(self): """印出Student物件內容""" print(self.name, '國語',self.mand, '數學',self.math, '社會',self.soc, '自然',self.nat, '英文',self.eng) def __str__(self): return ("{} '國語' {} '數學' {} '社會' {} '自然' {} '英文' {}" .format(self.name, self.mand, self.math, self.soc, self.nat, self.eng)) __repr__ = __str__ """ def setMath(self, math): if math < 0: self.math = 0 else: self.math = math """ def setMath(self, math): assert math >= 0, 'math不得小於0' self.math = math def getMath(self): return self.math def setIncome(self, income): assert income >= 0, 'income不得小於0' self.__home_income = income def getIncome(self): return self.__home_income def __add__(self, inc): return self.math + inc