在发送到Base构造函数之前修改参数值 [英] Modifying parameter values before sending to Base constructor

查看:156
本文介绍了在发送到Base构造函数之前修改参数值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

标题可能有点含糊不清,但我想不出更好的方法来说明这一点。

The title may be a bit ambiguous, but I couldn't think of a better way to word this.

我意识到我不能调用派生的构造函数在调用基础构造函数之前,我可以在将它们传递给基类之前以某种方式修改/创建参数值吗?

I realize that I can not call a derived constructor prior to calling a base constructor, but can I somehow modify/create parameters values prior to passing them to the base?

例如,

public enum InputType
{
    Number = 1,
    String = 2,
    Date = 3
}

public class BaseClass
{
    public BaseClass(InputType t)
    {
        // Logic
    }
}

public class DerivedClass : BaseClass
{
    public DerivedClass(int i)
        : base(value)
    // Can I do something to infer what value should be here?
    {
        // Logic
    }
}

如果我有一个派生类,可以推断出基础构造函数所需的值(在本例中, InputType.Number int有效,)有没有办法修改和/或创建在派生构造函数执行之前传递给基础构造函数的值?

If I have a derived class that can infer the value required for the base constructor (in this example, InputType.Number would be valid for an int,) is there a way to modify and/or create values that are passed to the base constructor prior to the derived constructor executing?

推荐答案

我希望你可以在基类构造函数的参数列表中调用静态方法。

I expect you could call static methods in the parameter list of the base class constructor.

public class DerivedClass : BaseClass
{
    public DerivedClass(int i)
        : base(ChooseInputType(i))
    {
    }

    private static InputType ChooseInputType(int i)
    {
        // Logic
        return InputType.Number;
    }
}

这篇关于在发送到Base构造函数之前修改参数值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆