子类'方法不会覆盖其超类方法

时间:2013-12-01 12:16:46

标签: python class methods super

为什么Sloop子类的方法get_info不会覆盖它的超类'方法?

class Boat( object ):
    def __init__( self, name, loa ):
        """Create a new Boat( name, loa )"""
        self.name= name
        self.loa= loa
    def get_info(self):
        print "Boat:", self.name, "Size:", self.loa
        print "This is printed correctly"

class Catboat( Boat ):
    def __init__( self, sail_area, * args ):
        """Create a new Catboat( sail_area, name, loa )"""
        super(Catboat,self).__init__( * args )
        self.main_area= sail_area
    def get_info(self):
        #print dir(self)
        print "Boat:", self.name, "Size:", self.loa, "Sail area:", self.main_area
        print "This one also"

class Sloop( Catboat ):
    def __init__( self, jib_area, * args ):
        """Create a new Sloop( jib_area, main_area, name, loa )"""
        super(Sloop,self).__init__( * args )
        self.jib_area= jib_area
    def get_info(self):
        print "Boat:", self.name, "Size:", self.loa, "Sail area:", self.main_area,             "Jib_area:", self.jib_area
        print "This should be printed, but it's not"

boat1 = Boat("Titanic", 20)
catboat1 = Catboat(50, "Titanic", 40)
sloop1 = Sloop(70, 60, "Titanic", 50)

boat1.get_info()
catboat1.get_info()
sloop1.get_info()

'''

打印:

船:泰坦尼克号码:20 这是正确打印的 船:泰坦尼克号码:40帆区:50 这一个也 船:泰坦尼克号码:50帆区:60 这也是

预期:

船:泰坦尼克号码:20 这是正确打印的 船:泰坦尼克号码:40帆区:50 这一个也 船:泰坦尼克号码:50帆区:60 Jib_area:70 这应该打印,但它不是

1 个答案:

答案 0 :(得分:1)

如果问题中给出的缩进是正确的,那么子类没有get_info()函数