重写父类方法

   所谓重写,就是子类中,有一个和父类相同名字的方法,在子类中的方法会覆盖掉父类中同名的方法,

#coding=utf-8
class Cat(object):
    def sayHello(self):
        print("halou-------1")

class Bosi(Cat):
    def sayHello(self):
        print("halou-------2")
bosi = Bosi()
bosi.sayHello()

 

    调用父类的方法

#coding=utf-8
class Cat(object):
    def _init_(self,name):
        self.name = name
        self.color = 'yellow'

class Bosi(Cat):
    def _init_(self,name):
        #调用父类的_init_方法1
        #Cat._init_(self,name)
        #调用父类的_init_方法2
        supper()._init_(name)
     def getName(self):
         return self.name
bosi = Bosi('xiaohua')
print(bosi.name)
print(bosi.color)

 

相关文章:

  • 2022-12-23
  • 2022-12-23
  • 2021-05-26
  • 2022-01-24
  • 2021-07-15
  • 2022-12-23
  • 2022-12-23
  • 2021-07-09
猜你喜欢
  • 2022-12-23
  • 2022-12-23
  • 2021-08-20
  • 2022-02-09
  • 2021-06-13
相关资源
相似解决方案