为什么C#从重写属性中获取值而不是覆盖属性?

Edw*_*uay 4 c# oop inheritance

我怀疑下面的代码输出:

(我是SmartForm对象并使用SmartForm中的方法).xml

相反,它输出:

(我是一个SmartForm对象并使用Item中的方法).xml

这是为什么?如何强制C#从覆盖属性中获取值?这就是我压倒财产的原因.

using System;

namespace TestInhersdk234
{
    public class Program
    {
        static void Main(string[] args)
        {
            SmartForm smartForm = new SmartForm();
            Console.ReadLine();
        }
    }

    public class SmartForm : Item
    {
        public SmartForm()
        {
            Console.WriteLine(FullXmlDataStorePathAndFileName);
        }

        public new string GetItemTypeIdCode
        {
            get
            {
                return String.Format("(I am a {0} object and using the method in SmartForm)", this.GetType().Name);
            }
        }
    }

    public class Item
    {
        public string FullXmlDataStorePathAndFileName
        {
            get
            {
                return GetItemTypeIdCode + ".xml";
            }
        }

        public string GetItemTypeIdCode
        {
            get
            {
                return String.Format("(I am a {0} object and using the method in Item)", this.GetType().Name);
            }
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

Jon*_*n B 13

你实际上并没有压倒一切.你在躲藏.要覆盖:

class MyBase
{
    public virtual void foo() {}
}

class MyClass : MyBase
{
    public override void foo() {}
}
Run Code Online (Sandbox Code Playgroud)


sec*_*ond 6

您需要将覆盖关键字添加到覆盖方法吗?