zoukankan      html  css  js  c++  java
  • 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()
    
  • 相关阅读:
    such用法
    divorce用法
    towel用法
    go for用法
    catch on用法
    incredibly用法
    mess用法
    dentist用法
    steer clear of用法
    incredible
  • 原文地址:https://www.cnblogs.com/yinwei-space/p/9275810.html
Copyright © 2011-2022 走看看