python: 多态与虚函数;

通过python的abc模块能够实现虚函数;

首先在开头from abc import   ABCMeta, abstractmethod

例子 :

#!/usr/bin/python
#coding=utf-8

from abc import ABCMeta, abstractmethod
class Base():
    __metaclass__=ABCMeta          #必须先声明

    def __init__(self):
        pass
    @abstractmethod              #虚函数
    def get(self):
        print 'base get'
        pass
class Derivel(Base):
    def get(self):
        print "Derivel get"

class Derivel2(Base):
    def get(self):
        print "Derivel2 get"

A = Derivel()
B = Derivel2()
A.get()
B.get()

猜你喜欢

转载自www.cnblogs.com/yinwei-space/p/9275810.html