class Person(object):
def __init__(self,id,name,sex,married):
if not isinstance(id,str):
raise Exception("ID must be srting")
self.id =id
if not isinstance(name,str):
raise Exception("name must be string")
self.name=name
if sex not in ('男','女'):
raise Exception("性别为男或女")
self.sex=sex
if married not in ('已婚','未婚'):
raise Exception("婚况只能未已婚和未婚")
self.married=married
def __get(self):
return self.__married
def __set(self,v):
self.__married=v
def __del(self):
del self.__married
married=property(__get,__set,__del)
def show(self):
print('ID:',self.ID)
print('name:',self.name)
print('sex:',self.sex)
print('married:',self.__married)
p1=Person('650302198305068965', '王尔康', '女','未婚')
p1.show()
代码报错,无法运行,望有人修改解决,python
- 写回答
- 好问题 0 提建议
- 追加酬金
- 关注问题
- 邀请回答
-
2条回答 默认 最新
- keenanli 2022-04-19 07:37关注
函数位置错了
class Person(object): def __init__(self, id, name, sex, married): if not isinstance(id, str): raise Exception("ID must be srting") self.id = id if not isinstance(name, str): raise Exception("name must be string") self.name = name if sex not in ('男', '女'): raise Exception("性别为男或女") self.sex = sex if married not in ('已婚', '未婚'): raise Exception("婚况只能未已婚和未婚") self.married = married def __get(self): return self.__married def __set(self, v): self.__married = v def __del(self): del self.__married married = property(__get, __set, __del) def show(self): print('ID:', self.id) print('name:', self.name) print('sex:', self.sex) print('married:', self.__married) p1 = Person('650302198305068965', '王尔康', '女', '未婚') p1.show()
本回答被题主选为最佳回答 , 对您是否有帮助呢?解决 无用评论 打赏 举报
悬赏问题
- ¥15 多尺度特征图提取和融合
- ¥15 微信小程序:渲染收货地址时页面不显示
- ¥20 win7 64位DirectShow提示初始化失败如何解决?
- ¥20 小规模孤立词识别系统设计
- ¥15 关于Java对接海康威视车牌识别一体机SDK是否需要固定外网的IP?
- ¥15 Linux扩容时,格式化卡住了:vgdispaly查看卷组信息,没有输出
- ¥18 关于#ubuntu#的问题:使用背景-工作职责内有七八台ubuntu系统的电脑,平时需要互相调取资料,想实现把这几台电脑用交换机组成一个局域网,来实现指定文件夹的互相调取和写入
- ¥20 求一个简易射频信号综测仪
- ¥15 esp8266 tally灯 接收端改为发射端
- ¥30 Labview代码调用access 数据库,相同代码其中一个调用不出来是为什么