慕函数8028368
2018-12-05 08:48
class Programer(object): hobby='Play Computer' def _inint_(self,name,age,weight): self.name=name self._age=age self.__weight=weight def get_weight(self): return self._weight if __name__=='__main__': programer=Programer('Albert',25,80) print(dir(programer)) print(programer.__dict__) print(programer.get_weight()) print(programer._Programer__weight) 执行完提示object() takes no parameters
class Programer(object):
hobby='Play Computer'
def __init__(self,name,age,weight):#init名字写错了
self.name=name
self._age=age
self.__weight=weight
def get_weight(self):
return self.__weight#这里的名字也漏了个下划线
if __name__=='__main__':
programer=Programer('Albert',25,80)
print(dir(programer))
print(programer.__dict__)
print(programer.get_weight())
print(programer._Programer__weight)
代码 第3行有问题:
def
_inint_(
self
,name,age,weight):
# __
init__(self)是构造函数哦!
应该是:
__def
init__(
self
, name, age, weight):
Python-面向对象
71236 学习 · 81 问题