Autosave_杨
2017-06-04 16:07
class Programer(object):
hobby='play computer'
def __init__(self,name,age,weight):
self.name=name
self._age=age
self.__weight=weight
def self_intro(self):
print 'my name is %s \nI am %s years old\n' % (self.name, self._age)
class BackendProgramer(Programer):
def __init__(self,name,age,weight,language):
super(BackendProgramer,self).__init__(name,age,weight)
self.language = language
def self_intro(self):
print 'my name is %s \nmy favourite language is %s\n' % (self.name, self.language)
def intro(i):
if isinstance(i,Programer):
print i.self_intro
if __name__=='__main__':
prog = Programer('Albert',25,'80')
back_prog = BackendProgramer('Alex',23,'80','Ruby')
intro(prog)
intro(back_prog)运行结果是:
<bound method Programer.self_intro of <__main__.Programer object at 0x030A5490>>
<bound method BackendProgramer.self_intro of <__main__.BackendProgramer object at 0x030A5510>>
17行出错!类方法self_intro不能直接这样调用,除非它本身是property一样的属性调用方法模式,那就可以不用加()
第17行,i.self_intro改为i.self_intro()
看不懂,不过我觉的把print i.self_intro() 改成 return i.self_intro() 应该就不会有空值了
为什么会返回none呢
原来是print i.self_intro忘记加()
Python-面向对象
71226 学习 · 83 问题
相似问题