Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/oop/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 这里如何避免代码重复?_C#_Oop_Code Duplication_Overloading_Duplication - Fatal编程技术网

C# 这里如何避免代码重复?

C# 这里如何避免代码重复?,c#,oop,code-duplication,overloading,duplication,C#,Oop,Code Duplication,Overloading,Duplication,我在一个类中有两种方法,一种是带有额外参数的方法 第一个: public override void CalcV(IV iv) { initializations otherOperations for (int i=0; i < NUM; ++i) { SomeOtherOperations double v = GetV(a,b,c); SomeOtherOperationsUsing_v

我在一个类中有两种方法,一种是带有额外参数的方法

第一个:

public override void CalcV(IV iv)
{
     initializations
     otherOperations

     for (int i=0; i < NUM; ++i)
     {
         SomeOtherOperations
         double v = GetV(a,b,c);
         SomeOtherOperationsUsing_v
     }

     restOfOperations
}
public override void CalcV(IV)
{
初始化
其他操作
对于(int i=0;i
第二点:

public override void CalcV(IV iv, int index)
{
     initializations
     otherOperations

     for (int i=0; i < NUM; ++i)
     {
         SomeOtherOperations
         double v = GetV(a,b,c, index);
         SomeOtherOperationsUsing_v
     }

     restOfOperations
}
public override void CalcV(IV-IV,int-index)
{
初始化
其他操作
对于(int i=0;i
正如您所看到的,唯一的区别是第一个调用带有3个参数的GetV(),第二个调用带有4个参数的GetV()重载

public override void CalcV(IV iv)
{
  return CalcV(iv, -1);
}
如何最好地避免代码重复


谢谢

猜一下GetV的功能(您需要更改此选项以适应:

public override void CalcV(IV iv)
{
     CalcV(iv, 0);
}


public override void CalcV(IV iv, int index)
{
     initializations
     otherOperations

     for (int i=0; i < NUM; ++i)
     {
         SomeOtherOperations
         double v = GetV(a,b,c, index);
         SomeOtherOperationsUsing_v
     }

     restOfOperations
}
public override void CalcV(IV)
{
CalcV(iv,0);
}
公共覆盖无效计算值(IV、IV、int索引)
{
初始化
其他操作
对于(int i=0;i
猜测GetV的功能(您需要更改此选项以适应:

public override void CalcV(IV iv)
{
     CalcV(iv, 0);
}


public override void CalcV(IV iv, int index)
{
     initializations
     otherOperations

     for (int i=0; i < NUM; ++i)
     {
         SomeOtherOperations
         double v = GetV(a,b,c, index);
         SomeOtherOperationsUsing_v
     }

     restOfOperations
}
public override void CalcV(IV)
{
CalcV(iv,0);
}
公共覆盖无效计算值(IV、IV、int索引)
{
初始化
其他操作
对于(int i=0;i
我假设
索引
以0为基础且为正:

public override void CalcV(IV iv, int index)
{
  initializations
  otherOperations

  for (int i=0; i < NUM; ++i)
  {
    SomeOtherOperations
    double v = index == -1 ? GetV(a, b, c) : GetV(a,b,c, index);
    SomeOtherOperationsUsing_v
  }

  restOfOperations
}

我假设
索引
以0为基础且为正:

public override void CalcV(IV iv, int index)
{
  initializations
  otherOperations

  for (int i=0; i < NUM; ++i)
  {
    SomeOtherOperations
    double v = index == -1 ? GetV(a, b, c) : GetV(a,b,c, index);
    SomeOtherOperationsUsing_v
  }

  restOfOperations
}

如果您使用的是.Net 4.0,则可以将其作为可选参数:

public override void CalcV(IV iv, int index = -1)
{
    ....
    double v = index > -1 ? GetV(a,b,c, index) : GetV(a,b,c);

    ....
}

如果您使用的是.Net 4.0,则可以将其作为可选参数:

public override void CalcV(IV iv, int index = -1)
{
    ....
    double v = index > -1 ? GetV(a,b,c, index) : GetV(a,b,c);

    ....
}
public override void CalcV(IV-IV,int?index=null)
{
初始化
其他操作
对于(int i=0;i
然后您可以删除第一个覆盖,这将处理两种情况。

公共覆盖void CalcV(IV-IV,int?index=null)
{
初始化
其他操作
对于(int i=0;i

然后您可以删除第一个覆盖,这将处理两种情况。

假设您不知道合理的默认值,一个非常简单的方法是:

public override void CalcV(IV iv)
{
    CalcV(iv, null);
}

public override void CalcV(IV iv, int? index)
{
     ...
     double v = index.HasValue ? GetV(a,b,c,index.Value) : GetV(a,b,c);
     ...
}

假设您不知道合理的违约,一个非常简单的方法是:

public override void CalcV(IV iv)
{
    CalcV(iv, null);
}

public override void CalcV(IV iv, int? index)
{
     ...
     double v = index.HasValue ? GetV(a,b,c,index.Value) : GetV(a,b,c);
     ...
}


当然,有一个默认的索引,你可以使用。当然,有一个默认的索引,你可以使用Yep,或者默认的参数-无论你的船漂浮在什么地方:)嗯……这可能不行,因为正如你所说的,你猜有三个参数的
GetV
与有四个参数的
GetV
是一样的。@Sani Huttunen-我同意-这就是答案开头的评论的目的-在原始问题中没有足够的细节,但给出了如何进行的一般要点…是的,e它是那个或默认参数-无论什么使你的船漂浮:)好吧。。。这可能不行,因为正如你所说的,你猜测有三个参数的
GetV
与有四个参数的
GetV
是一样的。@Sani Huttunen-我同意-这就是答案开头的评论的目的-在原始问题中没有足够的细节,但是给出了如何进行的一般要点…非常接近,但是没有更多的上下文,您无法确定GetV不接受
索引的负值。谢谢Dave!显然我需要一些咖啡!你能有一个负指数吗?我从来没有从哲学的角度考虑过:D…@DaveBish:负指数的概念是从最后开始倒数。也就是说,索引-1是最后一个元素。它非常接近,但是没有更多的上下文,您无法确定GetV不接受
索引的负值。谢谢Dave!显然我需要一些咖啡!你能有一个负指数吗?我从来没有从哲学的角度考虑过:D…@DaveBish:负指数的概念是从最后开始倒数。也就是说,指数-1是最后一个元素。其他一些答案假设1)GetV(a,b,c,0)=GetV(a,b,c)或/和2)index
是严格非负的。这在现在和将来都不一定是正确的。我的方法假设两者都不存在。其他一些答案假设1)GetV(a,b,c,0)=GetV(a,b,c)或/和2)index
是严格非负的。这在现在和将来都不一定是正确的。我的方法既不假设也不假设。这真的很好-这是最好的答案,因为它既简短又正确,没有任何假设。谢谢你,它是受你使用null的启发而产生的!这真的很好-这是最好的答案,因为它既简短又正确,没有任何假设。谢谢你,它的灵感来自你对null的使用!