为了账号安全,请及时绑定邮箱和手机立即绑定

使用没有抽象的 Base 方法

使用没有抽象的 Base 方法

C#
慕尼黑8549860 2021-08-22 18:00:58
下面的代码好吗?我在问自己界面中的Start()andStop()方法ICustomTimer是否可以作为位置。因为我在 Main 方法中需要它。这是一种代码异味,或者换句话说,调用没有抽象的基本方法的最佳实践是什么?Timer类没有可用于继承的接口。public interface ICustomTimer{    string Value { get; set; }    //Implementation in Timer    void Start();    //Implementation in Timer    void Stop();}public class CustomTimer : System.Timers.Timer, ICustomTimer{    public string Value { get; set; }}public Main(){    var customTimerObj = iocContainer.Get<ICustomTimer>();    customTimerObj.Start();}
查看完整描述

1 回答

?
缥缈止盈

TA贡献2041条经验 获得超4个赞

这是一个有效的用途,甚至是一个很好的用途,如果您只想在使用接口时调用类方法,否则您可以这样做:


public interface ICustomTimer

{

    string Value { get; set; }


    //Implementation in Timer

    void Start();


    //Implementation in Timer

    void Stop();

}


public class CustomTimer : System.Timers.Timer, ICustomTimer

{

    public string Value { get; set; }

    void ICustomTimer.Start() { this.Start(); }

    void ICustomTimer.Stop() { this.Stop(); }

}

这样你就可以做其他事情(在 Timer 类之前或发布方法调用)


查看完整回答
反对 回复 2021-08-22
  • 1 回答
  • 0 关注
  • 164 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信