在声明具有不同参数的构造函数时使用此方法

Using this when declaring constructors with different parameters
2021-05-11
  •  译文(汉语)
  •  原文(英语)

所以我有此类Battery和Enum BatteryType:

public enum BatteryType { LiIon, NiMH, NiCd };

public class Baterry
    {
        public string Model { set; get; }
        public int HoursIdle { set; get; }
        public int HoursTalk { set; get; }
        private BatteryType BatteryType;

        public Baterry(string model) : this(model, 0)
        {
        }

        public Baterry(string model, int hoursidle) : this(model, hoursidle, 0, null)
        {
        }

        public Baterry(string model, int hoursidle, int hourstalk, BatteryType batteryType)
        {
            this.Model = model;
            this.HoursIdle = hoursidle;
            this.HoursTalk = hourstalk;
            this.BatteryType = batteryType;

        }
    }

问题是

public Baterry(string model, int hoursidle) : this(model, hoursidle, 0, null)
{
}

我得到方法获取无效的参数错误.我该如何解决?

速聊1:
BatteryType是枚举,不能为null
速聊2:
您的班级拼写错误.
解决过程1

枚举不是C#中的可为空的类型,因此您不能设置BatteryTypenull.为您的枚举找到默认值或使您的枚举可为空:

private BatteryType? batteryType;

如果您只想将其0用作默认枚举值,则无需设置任何内容,因为0无论如何它是枚举的隐式默认值.

解决过程2

BatteryType不能为null-它是枚举.您可以在中定义其他项BatteryType,例如Undefined

解决过程3

更改为

public Baterry(string model, int hoursidle) : this(model, hoursidle, 0, 0)

枚举的默认值为0,而不是null.

解决过程4

BatteryType不是引用类型,因此您无法为其分配null.

您应该通过以下方式更改构造函数:

public Battery(string model, int hoursidle, int hourstalk, BatteryType? batteryType)
{
    this.Model = model;
    this.HoursIdle = hoursidle;
    this.HoursTalk = hourstalk;
    if(batteryType.HasValue)
        this.BatteryType = batteryType.Value;

}

构造函数现在接受nullable 作为最后一个参数BatteryType.

解决过程5

您应该使该枚举可为空(将其定义为BatteryType?)或定义BatteryType.None要使用的a.我更喜欢第二种选择.

解决过程6

我知道这不是您问题的直接答案,但是您是否考虑过仅制作一个带有可选参数的构造函数?您可以创建一个构造函数,使用户可以在单个构造函数中填写"模态","模态,hoursidle","modal,hoursidle,hourstalk"或"modal,hoursidle,hourstalk,batteryType".它可以使您轻松很多吗?

该行将简单地是:

public Baterry(string model, int hoursidle = 0, int hourstalk = 0, BatteryType batterytype = 0)
{
    this.Model = model;
    this.HoursIdle = hoursidle;
    this.HoursTalk = hourstalk;
    this.BatteryType = batterytype;
}

这是用默认值填写您自己不使用的每个参数.这也使您自己变得容易得多.

速聊1:
谢谢.我是OOP的新手,所以这是我第一次看到它.肯定会有用.
速聊2:
没问题,只需记住可选参数必须在最后,并且如果使用提供的示例,并且要发送"hourstalk",则还必须发送每个先前参数的值(在这种情况下,为模型和闲逛).查找msdn.microsoft.com/zh-cn/library/vstudio/dd264739.aspx,以获取更多信息.
速聊3:
谢谢:)稍稍修正了您的代码,batteryType应该为0,而不是null.
速聊4:
您自己也将其定义为null,所以我只复制了它:)无论如何,我将对其进行更改.

So I have this class Battery, and Enum BatteryType :

public enum BatteryType { LiIon, NiMH, NiCd };

public class Baterry
    {
        public string Model { set; get; }
        public int HoursIdle { set; get; }
        public int HoursTalk { set; get; }
        private BatteryType BatteryType;

        public Baterry(string model) : this(model, 0)
        {
        }

        public Baterry(string model, int hoursidle) : this(model, hoursidle, 0, null)
        {
        }

        public Baterry(string model, int hoursidle, int hourstalk, BatteryType batteryType)
        {
            this.Model = model;
            this.HoursIdle = hoursidle;
            this.HoursTalk = hourstalk;
            this.BatteryType = batteryType;

        }
    }

Problem is, at

public Baterry(string model, int hoursidle) : this(model, hoursidle, 0, null)
{
}

I get method gets invalid argument error. How should I fix that ?

Talk1:
BatteryType is enum, it can't be null
Talk2:
Your class is spelled incorrectly.
Solutions1

Enums are not a nullable type in C#, so you can't set BatteryType to null. Either find a default value for your enum or make your enum nullable:

private BatteryType? batteryType;

Should you want to just use 0 as your default enum value, you don't have to set anything, because 0 is the implicit default value for an enum anyway.

Solutions2

BatteryType cannot be null - it is enum. You can define additional item in BatteryType, e.g. Undefined

Solutions3

Change it to

public Baterry(string model, int hoursidle) : this(model, hoursidle, 0, 0)

The default value for an Enum is 0, not null.

Solutions4

BatteryType is not a reference type so you can't assign null to it.

You should change your constructor in this way:

public Battery(string model, int hoursidle, int hourstalk, BatteryType? batteryType)
{
    this.Model = model;
    this.HoursIdle = hoursidle;
    this.HoursTalk = hourstalk;
    if(batteryType.HasValue)
        this.BatteryType = batteryType.Value;

}

The constructor now accepts as last argument a nullable BatteryType.

Solutions5

You should make that enum nullable (defining it as BatteryType?) or defining a BatteryType.None to use. I prefer the second option.

Solutions6

I know it isn't the direct answer to your question, but have you thought about making just a single constructor with optional arguments? You can make a constructor where the user can either fill in "modal", "modal, hoursidle", "modal, hoursidle, hourstalk" or "modal, hoursidle, hourstalk, batteryType" in a single constructor. It could make it a lot easier on you?

The line would simply be:

public Baterry(string model, int hoursidle = 0, int hourstalk = 0, BatteryType batterytype = 0)
{
    this.Model = model;
    this.HoursIdle = hoursidle;
    this.HoursTalk = hourstalk;
    this.BatteryType = batterytype;
}

What this does is fill in every parameter that you yourself don't use with the default value. It makes it a lot easier on yourself as well.

Talk1:
Thanks. I'm new to OOP so that's the first time I see this. It will definitely be usefull.
Talk2:
No problem, just remember that the optional parameters have to be at the end, and if you use the example provided, and you want to send "hourstalk", you have to send the values for every previous parameters as well (in this case model and hoursidle). Look up msdn.microsoft.com/en-us/library/vstudio/dd264739.aspx for more information.
Talk3:
Thanks :) Just a little fix to your code, batteryType should be 0, not null.
Talk4:
You yourself defined it as null as well, so I just copied it :) Anyway, I will change it.
转载于:https://stackoverflow.com/questions/20217695/using-this-when-declaring-constructors-with-different-parameters

本人是.net程序员,因为英语不行,使用工具翻译,希望对有需要的人有所帮助
如果本文质量不好,还请谅解,毕竟这些操作还是比较费时的,英语较好的可以看原文

留言回复
我们只提供高质量资源,素材,源码,坚持 下了就能用 原则,让客户花了钱觉得值
上班时间 : 周一至周五9:00-17:30 期待您的加入