Python类 多态

多态:同一个方法由于对象的不同而产生不同的行为。

1.方法有多态,属性没有

2.多态存在的前提:继承和方法重写

#!/usr/bin/env python
# -*- coding: utf-8 -*-
# author:albert time:2019/4/18 0018


class Man:

    def eat(self):
        print('吃饭')


class Chinese(Man):

    def eat(self):
        print('筷子')


class Indian(Man):

    def eat(self):
        print('hands')


class English(Man):

    def eat(self):
        print('刀叉')


def Maneat(m):
    if isinstance(m,Man):
        m.eat()
    else:
        print('饿着')

Maneat(Chinese())
Maneat(Indian())
Maneat(English())

猜你喜欢

转载自www.cnblogs.com/zzm-blog/p/10732223.html
今日推荐