请问红框内代码为什么会报错?dbjebdjebdjjennskdnzhgavwuhwbkdkdknenx
2条回答 默认 最新
- 爱看书的小沐 2022-08-14 21:18关注
因为stu_no是类的成员变量,不是成员函数,后面不需要加括号“()”。
正确写法修改如下:stu.stu_no
测试的完整代码如下:
class Person(object): ''' a class Person ''' def __init__(self,name='',age=20,sex='man'): self.setName(name) self.setAge(age) self.setSex(sex) def setName(self,name): if not isinstance(name,str): raise Exception('name must be a string.') self._name=name def setAge(self,age): if type(age)!=int: raise Exception('age must be an integer.') self._age=age def setSex(self,sex): if sex not in ('man','woman'): raise Exception('sex must be "man" or "woman"') self._sex=sex def show(self): print(self._name, self._age, self._sex, sep='\n') class Student(Person): ''' a class Student ''' def __init__(self,name='test',age=30, sex='man', stu_no='20220001'): super().__init__(name,age,sex) self._stu_no = stu_no def setNo(self, stu_no): if not isinstance(stu_no, str): raise Exception('stu_no must be a string.') self._stu_no=stu_no def show(self): super().show() print(self._stu_no) if __name__=="__main__": stu=Student("Lily", 25, 'woman', '20220016') stu.setNo('20220055') print(stu._stu_no) stu._stu_no = '20220088' print(stu._stu_no) stu.show()
本回答被题主选为最佳回答 , 对您是否有帮助呢?解决 1无用
悬赏问题
- ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
- ¥15 有了解d3和topogram.js库的吗?有偿请教
- ¥100 任意维数的K均值聚类
- ¥15 stamps做sbas-insar,时序沉降图怎么画
- ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
- ¥15 关于#Java#的问题,如何解决?
- ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算
- ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
- ¥20 BAPI_PR_CHANGE how to add account assignment information for service line
- ¥500 火焰左右视图、视差(基于双目相机)