如何避免在不同的函数中使用相同的 switch 语句?
How to avoid using the same switch statement in different functions?
我有一个 class 的接口,接口的功能应该根据我使用这些功能的 class 的状态(枚举)而有所不同。
public class Test : Interface
{
Enum state = Enum.s1;
int test = 0
public void fn1(int x)
{
switch(state)
{
case Enum.s1:
{
test += x;
break;
}
...
}
}
public void fn2(int x, ref int y)
{
switch (state)
{
case Enum.s1:
{
y += x*test;
break;
}
...
}
}
public bool fn3()
{
switch (state)
{
case Enum.s1:
{
return DoStuff(test);
break;
}
...
}
}
}
是否可以进行一次评估并将其用于所有功能?
我是委托的新手,但是为 fn1/fn2/fn3 创建委托并在每次状态更改时填充所需函数可能是一个好的解决方案吗?
我问是因为我认为对此有更好的解决方案。
感谢您的帮助:)
I have an interface for a class and the functions of the interface should behave differently depending on the state (enum) of the class where i use the functions.
Is it possible to make a single evaluation and use it for all functions?
是也不是
否,用Enum和控制结构无法更好地解决。将 Enum 输入 switch/case 已经是您可以在代码中完成的最少工作量了。
是的,有可能做得更好。您在这里应该拥有的不是 1 个在枚举值上表现不同的实现。对于这个接口,你应该一直有 3 个不同的实现
一堆函数的不同行为,都引用相同的东西?这就是在继承期间覆盖的目的!
可能只有一种奇怪的情况,那就是如果您需要能够 改变 状态之间的 Enum/Convert。这也不是什么大问题,因为可以使用一个实例来初始化另一个实例。
看看 List<T>
class constructors. One of them takes a IEnumerable<T>
as input. All generic collections implement that interface, and even arrays are implicitly coverted into one (happens every time you use foreach loop, in fact). So they can all use each other as initializers. It also has a ToArray()
function.
我发誓某些 Stream 类 可以将其他 Stream 作为构造参数。一种将流相互包装起来的简单方法。但是找不到它。在任何情况下,大多数都将 Byte[]
作为 C-tor 参数。而且都可以expressed/implicitly转换成Byte[]
。奇怪的是,这种行为甚至没有在抽象基础 class 中定义。这只是大多数继承人所做的事情。
编辑:此外,如果您仍然需要其他代码的枚举值:只读 属性。与普通字段不同,它们可以添加到接口中。每个实现都可以 return 不同的硬编码值。
我有一个 class 的接口,接口的功能应该根据我使用这些功能的 class 的状态(枚举)而有所不同。
public class Test : Interface
{
Enum state = Enum.s1;
int test = 0
public void fn1(int x)
{
switch(state)
{
case Enum.s1:
{
test += x;
break;
}
...
}
}
public void fn2(int x, ref int y)
{
switch (state)
{
case Enum.s1:
{
y += x*test;
break;
}
...
}
}
public bool fn3()
{
switch (state)
{
case Enum.s1:
{
return DoStuff(test);
break;
}
...
}
}
}
是否可以进行一次评估并将其用于所有功能?
我是委托的新手,但是为 fn1/fn2/fn3 创建委托并在每次状态更改时填充所需函数可能是一个好的解决方案吗?
我问是因为我认为对此有更好的解决方案。
感谢您的帮助:)
I have an interface for a class and the functions of the interface should behave differently depending on the state (enum) of the class where i use the functions.
Is it possible to make a single evaluation and use it for all functions?
是也不是
否,用Enum和控制结构无法更好地解决。将 Enum 输入 switch/case 已经是您可以在代码中完成的最少工作量了。
是的,有可能做得更好。您在这里应该拥有的不是 1 个在枚举值上表现不同的实现。对于这个接口,你应该一直有 3 个不同的实现
一堆函数的不同行为,都引用相同的东西?这就是在继承期间覆盖的目的!
可能只有一种奇怪的情况,那就是如果您需要能够 改变 状态之间的 Enum/Convert。这也不是什么大问题,因为可以使用一个实例来初始化另一个实例。
看看 List<T>
class constructors. One of them takes a IEnumerable<T>
as input. All generic collections implement that interface, and even arrays are implicitly coverted into one (happens every time you use foreach loop, in fact). So they can all use each other as initializers. It also has a ToArray()
function.
我发誓某些 Stream 类 可以将其他 Stream 作为构造参数。一种将流相互包装起来的简单方法。但是找不到它。在任何情况下,大多数都将 Byte[]
作为 C-tor 参数。而且都可以expressed/implicitly转换成Byte[]
。奇怪的是,这种行为甚至没有在抽象基础 class 中定义。这只是大多数继承人所做的事情。
编辑:此外,如果您仍然需要其他代码的枚举值:只读 属性。与普通字段不同,它们可以添加到接口中。每个实现都可以 return 不同的硬编码值。