python面向对象编程三、继承和多态

继承

在OOP【面向对象编程——Object Oriented Programming】程序设计中,定义一个class的时候,可以从现有的class继承,新的class称为子类(Subclass),而被继承的class称为基类、父类或超类(Base class、Super class)

父类

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

C:\Users\33066\PycharmProjects\untitled\venv\Scripts\python.exe C:/Users/33066/Desktop/nine.py
Animal is running

Process finished with exit code 0

子类

class Animal(object):
    def run(self):
        print('animal is running')
class Dog(Animal):
    def run(self):
        print('dog is running')
class Cat(Animal):
    def run(self):
        print('dog is running')
animal=Animal()
dog=Dog()
cat=Cat()
animal.run()
dog.run()
cat.run()

C:\Users\33066\PycharmProjects\untitled\venv\Scripts\python.exe C:/Users/33066/Desktop/nine.py
animal is running
dog is running
dog is running

Process finished with exit code 0

继承

就是子类获得了父类的全部功能run()

class Animal(object):
    def run(self):
        print('Animal is running')
class Dog(Animal):
    pass
class Cat(Animal):
    pass
dog=Dog()
dog.run()
cat=cat()
cat.run()

C:\Users\33066\PycharmProjects\untitled\venv\Scripts\python.exe C:/Users/33066/Desktop/nine.py
animal is running
animal is running

Process finished with exit code 0

多态

类似于python的其他数据类型,list、dict、str,class所编写的类也是一种数据类型

用isinstance()可以判断变量是什么数据类# 多态
类似于python的其他数据类型,list、dict、str,class所编写的类也是一种数据类型

用isinstance()可以判断变量是什么数据类型
子类增加方法
当子类的方法与父类的方法同名时,直接调用子类的方法

class Animal(object):
    def run(self):
        print('animal is running')
class Dog(animal):
    def eat(self):
        print('dog is eat')
    def run(self):
        print('dog is running')
class Cat(animal):
    def eat(self):
        print('cat is eat')
    def run(self):
        print('cat is running')
dog=dog()
dog.eat()
dog.run()
cat=cat()
cat.eat()
cat.run()

C:\Users\33066\PycharmProjects\untitled\venv\Scripts\python.exe C:/Users/33066/Desktop/nine.py
dog is eat
dog is running
cat is eat
cat is running

Process finished with exit code 0

isinstance判断

class animal(object):
    def run(self):
        print('animal is running')
class Dog(animal):
    def eat(self):
        print('dog is eat')
    def run(self):
        print('dog is running')
class Cat(animal):
    def eat(self):
        print('cat is eat')
    def run(self):
        print('cat is running')
a = list() # a是list类型
b = animal() # b是Animal类型
c = Dog() # c是Dog类型
d = Cat()#d市cat类型
print(isinstance(a, list))
print(isinstance(b, animal))
print(isinstance(c, Dog))
print(isinstance(d, Cat))

C:\Users\33066\PycharmProjects\untitled\venv\Scripts\python.exe C:/Users/33066/Desktop/nine.py
True
True
True
True

Process finished with exit code 0

俩次输出run方法

class Animal(object):
    def run(self):
        print('Animal is running')
class Dog(Animal):
    def run(self):
        print('Dog is running')
class Cat(Animal):
    def run(self):
        print('Cat is running')
def run_(animal):
    animal.run()
    animal.run()
run_(Animal())
run_(Dog())
run_(Cat())
class Tortoise(Animal):
    def run(self):
        print('Tortoise is running')
run_(Tortoise())

C:\Users\33066\PycharmProjects\untitled\venv\Scripts\python.exe C:/Users/33066/Desktop/nine.py
Animal is running
Animal is running
Dog is running
Dog is running
Cat is running
Cat is running
Tortoise is running
Tortoise is running

Process finished with exit code 0
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值