欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

python面试题之阅读下面的代码,它的输出结果是什么?

程序员文章站 2023-11-18 19:47:10
答案 输出结果以注释的形式表示: a.go() # go A go! b.go() # go A go! # go B go! c.go() # go A go! # go C go! d.go() # go A go! # go C go! # go B go! # go D go! e.go() ......
class a(object):
    def go(self):
        print "go a go!"
    def stop(self):
        print "stop a stop!"
    def pause(self):
        raise exception("not implemented")

class b(a):
    def go(self):
        super(b, self).go()
        print "go b go!"

class c(a):
    def go(self):
        super(c, self).go()
        print "go c go!"
    def stop(self):
        super(c, self).stop()
        print "stop c stop!"

class d(b,c):
    def go(self):
        super(d, self).go()
        print "go d go!"
    def stop(self):
        super(d, self).stop()
        print "stop d stop!"
    def pause(self):
        print "wait d wait!"

class e(b,c): pass

a = a()
b = b()
c = c()
d = d()
e = e()

# 说明下列代码的输出结果

a.go()
b.go()
c.go()
d.go()
e.go()

a.stop()
b.stop()
c.stop()
d.stop()
e.stop()

a.pause()
b.pause()
c.pause()
d.pause()
e.pause()

答案

输出结果以注释的形式表示:

a.go()
# go a go!

b.go()
# go a go!
# go b go!

c.go()
# go a go!
# go c go!

d.go()
# go a go!
# go c go!
# go b go!
# go d go!

e.go()
# go a go!
# go c go!
# go b go!

a.stop()
# stop a stop!

b.stop()
# stop a stop!

c.stop()
# stop a stop!
# stop c stop!

d.stop()
# stop a stop!
# stop c stop!
# stop d stop!

e.stop()
# stop a stop!

a.pause()
# ... exception: not implemented

b.pause()
# ... exception: not implemented

c.pause()
# ... exception: not implemented

d.pause()
# wait d wait!

e.pause()
# ...exception: not implemented

为什么提这个问题

因为面向对象的编程真的真的很重要。不骗你。答对这道问题说明你理解了继承和python中super函数的用法。

本文首发于python黑洞网,博客园同步跟新