python基础-多态、封装、继承

参考资料:https://www.liaoxuefeng.com/wiki/001374738125095c955c1e6d8bb493182103fac9270762a000/001386820044406b227b3e751cc4d5190420d17a2dc6353000

继承(Inheritance):以普通的类为基础建立专门的类的对象;

多态(polymorphism):意味着可以对不同类的对象使用同样的操作,它们会像被“施了魔法一般”工作;

封装(Encapsulation):对外部世界隐藏对象的工作细节;

1、继承

class Animal(object):
    def run(self):
        print 'Animal is running..'

class Dog(Animal):
    pass
class Cat(Animal):
    pass

Dog和Cat继承了Animal,同时继承了Animal的方法

class Dog(Animal):
      def run(self):
        print 'Dog is running...'
      def eat(self):
        print 'Eating meat..'  
class Cat(Animal): 
      def run(self):
        print 'Cat is running'
当子类和父类都存在相同的run()方法时,我们说,子类的run()覆盖了父类的run(),在代码运行的时候,总是会调用子类的run()。这样,我们就获得了继承的另一个好处:多态。

 

转载于:https://www.cnblogs.com/JuanZi-Sunny/p/10002718.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值