首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用接口调用2个方法

使用接口调用2个方法
EN

Stack Overflow用户
提问于 2015-05-29 02:02:20
回答 1查看 21关注 0票数 0

我想声明一个接口,从它继承的人将自动执行2个操作1-将在函数开始时写入日志

2-将在函数结束时写入日志,这些操作将自动完成,程序员应该做的唯一一件事就是在接口上实现函数,有些人知道我应该如何实现它?

EN

回答 1

Stack Overflow用户

发布于 2015-05-29 02:10:46

您不能使用接口做到这一点,但是您可以提供一个接口的实现,该接口包装另一个实现并记录自己的函数调用。例如:

代码语言:javascript
复制
public interface IExample
{
    void DoSomething(string parameter1);
}

public class ExampleImpl : IExample
{
    private IExample actualImplementation;

    public ExampleImpl(IExample actualImplementation)
    {
        this.actualImplementation = actualImplementation;
    }

    public void DoSomething(string parameter1)
    {
        //Code to log function begin here

        this.actualImplementation.DoSomething(parameter1);

        //Code to log function end here
    }
}

现在,假设另一个程序员也实现了该接口,例如,假设他们的实现称为AnotherProgrammersImplementation

代码语言:javascript
复制
IExample thisObjectLogsFunctionCalls = new ExampleImpl(new AnotherProgrammersImplementation());

thisObjectLogsFunctionCalls.DoSomething("test string");
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/30513930

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档