永远不要创造静态,除非你真的,真的必须如果你这样做,你将为自己做好准备.你使测试更难,你使线程冲突的可能性(正如你所描述的)发生得更高.
如果必须在类中设置状态然后调用方法,而不是仅将值作为参数传递给方法,请将其设置为非静态类.此外,您最好通过构造函数传递值,而不是使用属性.
话虽如此,我对您的问题的处理方法是创建一个POCO来保存结果数据并使用静态方法进行计算.使用C#6语法,这将看起来像:
public class BreakTimeResult { public BreakTimeResult(int breakFromSec, int breakUntilSec) { BreakFromSec = breakFromSec; BreakUntilSec = breakUntilSec; } public int BreakFromSec { get; } public int BreakUntilSec { get; } } public static class BreakTimeCalculator { public static BreakTimeResult CalculateBreakFromAndBreakeUntil(int customBreakSec, int fromSec, int untilSec, int equlizer) { var taskLength = untilSec - fromSec; var middleOfTask = fromSec + (taskLength / 2); var secondsToMoveLeft = middleOfTask % 300; var amountEqualizers = customBreakSec / equlizer; var fiftyFifty = amountEqualizers % 2 == 0; var leftSideEqualizers = fiftyFifty ? amountEqualizers / 2 : (amountEqualizers / 2) + 1; var breakFromSec = middleOfTask - secondsToMoveLeft - (leftSideEqualizers * equlizer); var breakUntilSec = breakFromSec + customBreakSec; return new BreakTimeResult(breakFromSec, breakUntilSec); } }