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())

 

posted @ 2019-04-18 20:44  文二1234  阅读(517)  评论(0编辑  收藏  举报