简体   繁体   English

我可以调用基类的派生方法吗?

[英]Can I call a derived method from base class?

I have several classes, some of which are abstract that I want the base class version of the method to call the most derived version of another method in the same class which then works it's way up the chain and doing the 'complete' job of the method. 我有几个类,其中一些是抽象类,我希望该方法的基类版本调用同一类中另一个方法的最派生版本,然后该类沿链条向上进行并完成“完成”工作。方法。 The reason why I have base and derived methods is because the different levels have different access to information. 我之所以拥有基本方法和派生方法,是因为不同级别对信息的访问不同。 If an 'Order' object is null I don't want to have to test for null before I try to get the information. 如果“ Order”对象为null,则在获取信息之前,我不需要测试null。 The result is a series of 'cascading' classes with each derived version of the method calling the base method and then building on what the base method is doing to fulfil its objective. 结果是一系列“级联”类,该方法的每个派生版本调用基础方法,然后在基础方法为实现其目标所做的工作上建立基础。

    public abstract class EmailTemplates
    {
        ....
        protected virtual string ReplaceVariables(KeyValuePair<string, string> namevalue, string body)
        {

            // This builds an email body
            protected void BuildBody()
            {
                if(NamesValues != null)
                {
                    foreach (KeyValuePair<string, string> namevalue in NamesValues)
                    {
                        // This gives back eg "order details"
                        string subsectionName;
                        // Test if this value is a subsection
                        // If subsection not in the list, keep unchanged
                        if (Subsections.TryGetValue(namevalue.Key, out subsectionName))
                        {
                            // This retrieves the subsection details
                            this.emailBody = this.emailBody.Replace(namevalue.Value, GetSubsection(subsectionName, namevalue.Value));
                        }
                        // This is a regular variable not a subsection
                        else
                        {
                            this.emailBody = ReplaceVariables(namevalue, this.emailBody);
                        }
                    }
                }
            }
        }
        protected virtual string ReplaceVariables(KeyValuePair<string, string> namevalue, string body)
        {
            switch(namevalue.Key)
            {
                case "url": 
                    body = body.Replace(namevalue.Value, Url);
                    break;
                case "username":
                    body = body.Replace(namevalue.Value, HttpContext.Current.User.Identity.Name);
                    break;     
            }
            return body;
        }
        ....
    }

    public abstract class CustomerEmailTemplates : EmailTemplates
    {
        ...
        protected new string ReplaceVariables(KeyValuePair<string, string> namevalue, string body)
        {
            body = base.ReplaceVariables(namevalue, body);

            switch (namevalue.Key)
            {
                case "forename":
                    // If they don't have a profile, just use the username
                    if ((Profile != null) && (Profile.IsAnonymous || Profile.DeliveryAddress1.FirstName == null || Profile.DeliveryAddress1.FirstName == ""))
                    {
                        body = body.Replace(namevalue.Value, Username);
                    }
                    // If user is changing their password, etc.
                    else if (Profile != null)
                    {
                        body = body.Replace(namevalue.Value, Profile.DeliveryAddress1.FirstName);
                    }
                    // To display template to admin don't replace anything
                    break;
                case "surname":
                    // If they don't have a profile, just use nothing as username will already be there
                    if ((Profile != null) && (Profile.IsAnonymous || Profile.DeliveryAddress1.LastName == null || Profile.DeliveryAddress1.LastName == ""))
                    {
                        body = body.Replace(namevalue.Value, "");
                    }
                    else if (Profile != null)
                    {
                        body = body.Replace(namevalue.Value, Profile.DeliveryAddress1.LastName);
                    }

                    // To display template to admin don't replace anything
                    break;
            }
            return body;
        }
        ...
    }

    public class OrderEmailTemplates : CustomerEmailTemplates
    {
        ...
        protected new string ReplaceVariables(KeyValuePair<string, string> namevalue, string body)
        {
            body = base.ReplaceVariables(namevalue, body);

            switch (namevalue.Key)
            {        
                case "customerEmail":
                    body = body.Replace(namevalue.Value, Order.CustomerEmail);
                    break;
                case "orderID":
                    body = body.Replace(namevalue.Value, Order.ID.ToString());
                    break;
                ....
            }
        ...
    }

Sorry for the code dump but I am unsure of how to make it (much) smaller. 对不起,代码转储,但我不确定如何使它变小得多。 What I am looking to have happen is for BuildEmailBody() to go down to the last derived class calling OrderEmailTemplates.ReplaceVariables() and work its way back to the base class but right now it is just calling the EmailTemplates.ReplaceVariables() and not replacing all the values I want it to. 我希望发生的事情是让BuildEmailBody()下降到最后一个调用OrderEmailTemplates.ReplaceVariables()派生类,并返回到基类,但现在它只是调用EmailTemplates.ReplaceVariables()而不是替换所有我想要的值。

  1. Virtual methods 虚方法
  2. Override 覆盖
  3. Base 基础

     public abstract class EmailTemplates { protected void BuildBody() { ReplaceVariables(); } protected virtual string ReplaceVariables() { //code } } public abstract class CustomerEmailTemplates : EmailTemplates { protected override string ReplaceVariables() { //code base.ReplaceVariables(); } } public class OrderEmailTemplates : CustomerEmailTemplates { protected override string ReplaceVariables() { //code base.ReplaceVariables(); } } 

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

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