python: 多态与虚函数;,,通过python的a


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

首先在开头from abc import ABCMeta, abstractmethod

例子 :

#!/usr/bin/python#coding=utf-8from abc import ABCMeta, abstractmethodclass Base():    __metaclass__=ABCMeta          #必须先声明    def __init__(self):        pass    @abstractmethod              #虚函数    def get(self):        print ‘base get‘        passclass 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()

python: 多态与虚函数;

评论关闭