使用没有抽象的 Base 方法

下面的代码好吗?我在问自己界面中的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();

}


慕尼黑8549860
浏览 165回答 1
1回答

缥缈止盈

这是一个有效的用途,甚至是一个很好的用途,如果您只想在使用接口时调用类方法,否则您可以这样做:public interface ICustomTimer{&nbsp; &nbsp; string Value { get; set; }&nbsp; &nbsp; //Implementation in Timer&nbsp; &nbsp; void Start();&nbsp; &nbsp; //Implementation in Timer&nbsp; &nbsp; void Stop();}public class CustomTimer : System.Timers.Timer, ICustomTimer{&nbsp; &nbsp; public string Value { get; set; }&nbsp; &nbsp; void ICustomTimer.Start() { this.Start(); }&nbsp; &nbsp; void ICustomTimer.Stop() { this.Stop(); }}这样你就可以做其他事情(在 Timer 类之前或发布方法调用)
打开App,查看更多内容
随时随地看视频慕课网APP