根据参数值重载方法?

发布于 2024-10-28 07:00:57 字数 609 浏览 3 评论 0原文

我有一个类,我希望类构造函数根据第一个参数的值而变化。

  public class Calc
    {
        public Calc( Operator calcOpr = Operator.Concat, string sourceName, string newString )
        {
            calcOperator = calcOpr;
            sourceType = dataType;
        }

        public Calc( Operator calcOpr = Operator.PadLeft,  int startindex, int  count )
        {
            calcOperator = calcOpr;
            sourceType = dataType;
        }

现在我知道上面的代码不是有效的代码,但作为伪代码,它确实显示了我想要实现的目标。
有没有某种方法可以根据参数值重载类构造函数?

编辑:我想这样做,以便实例化类时所需的参数根据不同的 Operator 值进行更改。

I have a class and I want the class constructor to vary according to the value of the first parameter.

  public class Calc
    {
        public Calc( Operator calcOpr = Operator.Concat, string sourceName, string newString )
        {
            calcOperator = calcOpr;
            sourceType = dataType;
        }

        public Calc( Operator calcOpr = Operator.PadLeft,  int startindex, int  count )
        {
            calcOperator = calcOpr;
            sourceType = dataType;
        }

Now I KNOW the above is not valid code, but as pseudocode it does show what I want to achieve.
Is there some way of getting class constructors overloaded based on the parameter values?

EDIT: I want to do this so that that the parameters required when instantiating the class are changed according to different Operator values.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

丑丑阿 2024-11-04 07:00:57

你可以这样实现它:

public class Calc
    {
        public Calc(string sourceName, string newString )
        {
            calcOperator = Operator.Concat;
            sourceType = dataType;
        }

        public Calc(int startindex, int  count )
        {
            calcOperator = Operator.PadLeft;
            sourceType = dataType;
        }
}

You could implement it that way:

public class Calc
    {
        public Calc(string sourceName, string newString )
        {
            calcOperator = Operator.Concat;
            sourceType = dataType;
        }

        public Calc(int startindex, int  count )
        {
            calcOperator = Operator.PadLeft;
            sourceType = dataType;
        }
}
无可置疑 2024-11-04 07:00:57

我认为这里使用的一个好的模式是工厂。

CalcFactory 允许调用者明确了解 Calc 的具体目标。

public static class CalcFactory
{
   public static Calc createConcatCalc(string sourceName, string newString)
   {
       // call explicit constructors
   }

   public static Calc createPadLeftCalc(int startindex, int count)
   {
       // call explicit constructors
   }
}

该工厂可以在内部使用@Roflcoptr的构造函数。

I think that a good pattern to use here would be a factory.

A CalcFactory would allow callers to know explicitly the concrete objective of the Calc.

public static class CalcFactory
{
   public static Calc createConcatCalc(string sourceName, string newString)
   {
       // call explicit constructors
   }

   public static Calc createPadLeftCalc(int startindex, int count)
   {
       // call explicit constructors
   }
}

This factory could use @Roflcoptr's constructors internally.

送你一个梦 2024-11-04 07:00:57

示例代码有点偏离,因为它们都有不同的签名(第二个参数是 string 与 int)。因此传递 int 或 string 就已经选择了正确的重载。

当您想要具有相同方法签名的不同行为时,只需打开枚举:

switch(calcOpr){
  case Operator.Concat: 
    calcOperator = calcOpr;
    sourceType = dataType;
    break;

  case Operator.Padleft: 
    calcOperator = calcOpr;
    sourceType = dataType;
    break;

  default: 
    // throw error?

}

编辑(到您的编辑)

方法具有固定签名,因此如果您希望有 1 个构造函数来创建基于运算符的正确类型,您不必在其中传递所有可能的参数。 (错误代码):

public Calc( Operator calcOpr, string sourceName, string newString,  int startindex, int  count )
{
  switch(calcOpr){
    case Operator.Concat: 
       // validate sourceName and newString

    case Operator.Padleft
       // validate startindex and count

}

闻起来不好。

我想我会选择单独的方法而不是构造函数重载。 (静态,因此您可以从类型中调用它们)

public static Calc GetConcat(string SourceName, string newString){
  Class c = new Class();
  c.calcOperator = Operator.Concat;
  c.sourcetype = dataType; //where does this one come from? 
  //etc..
  return c;
}

public static Calc GetPadLeft(int startindex, int  count){
  Class c = new Class();
  c.calcOperator = Operator.PadLeft;
  c.sourcetype = dataType; //where does this one come from? 
  // etc
  return c;
}

此外,您可能希望考虑为 Concat 和 PadLeft 创建派生类,这些类派生自 Calc 和重载方法/添加特定属性。事实上,我认为这就是我会做的,但你必须详细说明你正在做什么。

The example code is a bit off, since they both have a different signature (string vs int for the second argument). So passing and int or string would already select the right overload.

When you want different behaviour with the same method signature, just switch on the enum:

switch(calcOpr){
  case Operator.Concat: 
    calcOperator = calcOpr;
    sourceType = dataType;
    break;

  case Operator.Padleft: 
    calcOperator = calcOpr;
    sourceType = dataType;
    break;

  default: 
    // throw error?

}

Edit (to your edit)

A method has a fixed signature, so if you'd want to have 1 constructor that creates the right type based on the Operator, you'd have t pass all possible arguments in there. (bad code) :

public Calc( Operator calcOpr, string sourceName, string newString,  int startindex, int  count )
{
  switch(calcOpr){
    case Operator.Concat: 
       // validate sourceName and newString

    case Operator.Padleft
       // validate startindex and count

}

Doesn't smell good.

I think I would go for separate method instead of constructor overloads. (Static so you can call them from the type)

public static Calc GetConcat(string SourceName, string newString){
  Class c = new Class();
  c.calcOperator = Operator.Concat;
  c.sourcetype = dataType; //where does this one come from? 
  //etc..
  return c;
}

public static Calc GetPadLeft(int startindex, int  count){
  Class c = new Class();
  c.calcOperator = Operator.PadLeft;
  c.sourcetype = dataType; //where does this one come from? 
  // etc
  return c;
}

Also you may want to look into creating derived classes for Concat and PadLeft that derive from Calc and overload methods/ add specific properties. Actually I think that's what I would do but you'd have to tell a bit more about what you're doing exactly.

短暂陪伴 2024-11-04 07:00:57

也许您可以尝试使用可变参数的构造函数。

public class Calc
{
    public Calc( Operator calcOpr, params object[] values)
    {
    switch(op)
    {
    case Op.CONCAT:
        Concat(values[0],values[1]);
    case Op.PADLEFT:
        PadLeft(values[0],values[1],values[2]);
    }
    }

    public void Concat(string str1, string str2)
    {
    }
    public void PadLeft(string str1, int startindex, int  count )
    {
    }

May be You can try a constrcutor with variable arguments.

public class Calc
{
    public Calc( Operator calcOpr, params object[] values)
    {
    switch(op)
    {
    case Op.CONCAT:
        Concat(values[0],values[1]);
    case Op.PADLEFT:
        PadLeft(values[0],values[1],values[2]);
    }
    }

    public void Concat(string str1, string str2)
    {
    }
    public void PadLeft(string str1, int startindex, int  count )
    {
    }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文