【问题标题】:Overriding classes in c# and accessing a virtual method which uses an override method在 C# 中覆盖类并访问使用覆盖方法的虚拟方法
【发布时间】:2016-04-19 10:39:56
【问题描述】:

我是第一次使用 C# 中的重写方法,我认为我对基础知识有很好的掌握。但是我的方法稍微复杂一些,并且在方法中调用了一些方法。

我实际上是想通过找到一种使用基类方法但调用基类方法使用的覆盖方法的方法来节省自己的代码编写。

这是我的问题的一个例子,因为我可能解释得不太好:

namespace exampleCode
{
    public class BaseClass : AnotherClass
    {
        public static string OrderStorageContainer = "Deliverables";

        public virtual string CustomerStorageContainer = "CustomerDocs";

        public virtual string GetSomething(typeStorage st, string FileName)
        {
            if (String.IsNullOrWhiteSpace(FileName))
            {
                throw new ArgumentNullException("FileName", "The filename of the file should be supplied!");
            }

            var c = GetReference(GetName(st));        
            return c.ToString();
        }
        public virtual string GetName(typeStorage st)
        {
            switch (st)
            {
                case typeStorage.CustomerProvided:
                    return CustomerStorageContainer.ToLower();
                case typeStorage.SystemGenerated:
                    return OrderStorageContainer.ToLower();
            }
            throw new InvalidOperationException("No container defined for storage type: " + st.ToString());
        }

        public virtual string GetReference()
        {
            //does stuff
        }
    }

    public class DervivedClass : BaseClass
    {
        public static string QuoteStorageContainer = "QuoteDeliverables";

        public override string CustomerStorageContainer = "QuoteCustomerDocs";

        public override string GetSomething(typeStorage st, string FileName)
        {
            if (String.IsNullOrWhiteSpace(FileName))
            {
                throw new ArgumentNullException("FileName", "The filename of the file should be supplied!");
            }

            var c = GetReference(GetName(st));        
            return c.ToString();
        }

        public override string GetName(typeStorage st)
        {
            switch (st)
            {
                case typeStorage.CustomerProvided:
                    return CustomerStorageContainer.ToLower();
                case typeStorage.SystemGenerated:
                    return QuoteStorageContainer.ToLower();
            }
            throw new InvalidOperationException("No container defined for storage type: " + st.ToString());
        }

    }
}

基本上,我希望 Derived 类使用覆盖 GetSomething 方法。但是,我希望它使用覆盖的GetName() 方法的结果调用基类GetReference() 方法。

这是正确的路线吗?我一直发现很难在网上找到类似的示例。

任何指针都会很棒!

【问题讨论】:

  • 对我来说是正确的,只要您不覆盖 GetReference 方法,您将调用基本方法。如果你想覆盖它,你仍然可以使用base.GetReference()

标签: c# class methods overriding


【解决方案1】:

您的实现似乎是正确的。除非您不覆盖 DerivedClass 中的 GetReference() 方法,否则它将是正确的。

如果您希望var c = GetReference(GetName(st)); 这一行始终调用基类,则应采取预防措施,将其写为base.GetReference(GetName(st))

【讨论】:

  • 既然代码看起来是正确的,不应该把这个问题移到 CodeReview 网站吗?
  • 不在当前状态,因为示例代码 (BaseClass : AnotherClass) 将偏离主题。
【解决方案2】:

我认为你在正确的轨道上。您最好使用 base 和 this 以避免混淆并强制编译器使用您想要的实现。

【讨论】:

    猜你喜欢
    • 2011-05-21
    • 2018-01-28
    • 2013-01-15
    • 2012-06-19
    • 2012-02-19
    • 1970-01-01
    • 2013-06-18
    • 2014-03-18
    • 1970-01-01
    相关资源
    最近更新 更多