计算机科学中,包装函数(英语:Wrapper function)是一种计算机程序中的函数,它存在的主要目的就是用来调用另一个函数。在面向对象编程中,它又被称为方法委任(method delegation)。它的存在有多种功能:可以被当成适配器模式来使用,可以当成错误检查程序,也可以被当成多重继承来使用。

程序示例

public class StackSet implements Stack, Set {

    private LinkedList stack;
    private HashSet set;
    public boolean push(Object o) {
        if (set.add(o)) return stack.push(o);
        else return false;
    }
    public Object pop() {
        Object o = stack.pop();
        set.remove(o);
        return o;
    }
    public boolean contains(Object o) {
        return set.contains(o);
    }

}

Wikiwand in your browser!

Seamless Wikipedia browsing. On steroids.

Every time you click a link to Wikipedia, Wiktionary or Wikiquote in your browser's search results, it will show the modern Wikiwand interface.

Wikiwand extension is a five stars, simple, with minimum permission required to keep your browsing private, safe and transparent.