标签:测试 sharp get __init__ == span erro art error
练习
请把下面的Student对象的gender字段对外隐藏起来,用get_gender()和set_gender()代替,并检查参数有效性:
# -*- coding: utf-8 -*- class Student(object): def __init__(self, name, gender): self.__name = name self.__gender = gender def set_gender(self,gender): if gender==‘male‘or gender==‘female‘: self.__gender=gender else: raise TypeError("输入性别错误") def get_gender(self): return self.__gender # 测试: bart = Student(‘Bart‘, ‘male‘) if bart.get_gender() != ‘male‘: print(‘测试失败!‘) else: bart.set_gender(‘female‘) if bart.get_gender() != ‘female‘: print(‘测试失败!‘) else: print(‘测试成功!‘)
注意:
if gender==‘male‘or gender==‘female‘:
标签:测试 sharp get __init__ == span erro art error
原文地址:https://www.cnblogs.com/zuxing/p/8971795.html