简体   繁体   English

在C#中调用基类构造函数

[英]Calling base class constructor in c#

I have the following classes: 我有以下课程:

public abstract class BusinessRule
{
    public string PropertyName { get; set; }
    public string ErrorMessage { get; set; }

    public BusinessRule(string propertyName)
    {
        PropertyName = propertyName;
        ErrorMessage = propertyName + " is not valid";
    }

    public BusinessRule(string propertyName, string errorMessage)
        : this(propertyName)
    {
        ErrorMessage = errorMessage;
    }
 }

And

public class ValidateId : BusinessRule
{
    public ValidateId(string propertyName) : base(propertyName)
    {
        ErrorMessage = propertyName + " is an invalid identifier";
    }

    public ValidateId(string propertyName, string errorMessage)
        : base(propertyName)
    {
        ErrorMessage = errorMessage;
    }

    public override bool Validate(BusinessObject businessObject)
    {
        try
        {
            int id = int.Parse(GetPropertyValue(businessObject).ToString());
            return id >= 0;
        }
        catch
        {
            return false;
        }
    }
}

And

public class Customer : BusinessObject
{
    public Customer()
    {
        AddRule(new ValidateId("CustomerId"));
    }
 }

From within the Customer class I am adding a new business rule - ValidateId by calling it's constructor, which is fine, but then ValidateId's constructor is calling the base class constructor, this is where I start to get a little confused. 在Customer类中,我通过调用它的构造函数来添加新的业务规则-ValidateId,这很好,但是ValidateId的构造函数正在调用基类构造函数,这使我开始感到困惑。

Why do I need to call the base class constructor when ValidateId constructor can do what I am trying to achieve? 当ValidateId构造函数可以完成我想实现的目标时,为什么需要调用基类构造函数? And both ValidateId constructor and the base constructor is setting the Error Message to something: ValidateId构造函数和基本构造函数都将错误消息设置为:

This is in base constructor: 这是在基本构造函数中:

        PropertyName = propertyName;
        ErrorMessage = propertyName + " is not valid";

and this is in ValidateId constructor: 这是在ValidateId构造函数中:

ErrorMessage = propertyName + " is an invalid identifier";

Which one of the ErrorMessage will be used to display the error to the user? ErrorMessage的哪一个将用于向用户显示错误?

Also if I remove the : base(propertyName) and not call the base constructor I get BusinessRule does not contain parameterless constructor error message, obviously I can just add a parameterless constructor to the Businessrule and implement everything in ValidateId constructor but I want to know what are the advantages/implications of calling or not calling the base constructor? 另外,如果我删除: base(propertyName)而不调用基本构造函数,我得到的BusinessRule不包含无参数构造函数错误消息,显然我可以将无参数构造函数添加到Businessrule并在ValidateId构造函数中实现所有功能,但是我想知道什么或不调用基本构造函数的优点/含义是什么?

Thanks 谢谢

Why do I need to call the base class constructor when ValidateId constructor can do what I am trying to achieve? 当ValidateId构造函数可以完成我想实现的目标时,为什么需要调用基类构造函数?

You always have to go through a constructor at every level of the inheritance hierarchy - as otherwise any "skipped" levels wouldn't have a chance to initialize themselves and validate the parameters public ValidateId(string propertyName) : base(propertyName) { ErrorMessage = propertyName + " is an invalid identifier"; 始终必须在继承层次结构的每个级别上都要经过一个构造函数-否则,任何“跳过”级别都将没有机会初始化自身并验证参数public ValidateId(string propertyName):base(propertyName){ErrorMessage = propertyName +“是无效的标识符”; }. }。

Imagine if you could create a class deriving from FileStream which could skip the existing FileStream constructors - what file would that represent? 想象一下,如果您可以创建一个从FileStream派生的类,而该类可以跳过现有的FileStream构造函数-那代表什么文件? Where would it get its data from? 它会从哪里获得数据?

It seems to me that ValidateId(string propertyName) should be implemented as: 在我看来ValidateId(string propertyName)应该实现为:

public ValidateId(string propertyName)
    : base(propertyName, propertyName + " is an invalid identifier")
{
}

Only one piece of code needs to be setting the error message - and personally I'd actually make the setters private within BusinessRule , or even use a separate readonly field and only provide a getter at all. 只有一条代码需要被设置错误消息-而我个人倒实际上使制定者中私有BusinessRule ,甚至使用一个单独的只读字段,并只提供一个getter可言。

Just create BusinessRule() constructor without paramters. 只需创建不带参数的BusinessRule()构造函数即可。 Call base() in validateId() 在validateId()中调用base()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM