Accessing a static property of a child in a parent method

前端 未结 9 1323
孤城傲影
孤城傲影 2021-01-21 06:07

Say I have the following code:

class Parent
{

    static string MyField = \"ParentField\";

    public virtual string DoSomething()
    {
        return MyField         


        
9条回答
  •  花落未央
    2021-01-21 06:28

    You have to handle this in Child, too:

    class Child
    {
        static new string MyField = "ChildField";
    
        public override string DoSomething()
        {
             return MyField;
        }
    }
    

    That being said, using a single virtual property would probably be a cleaner design, since you wouldn't need the "new" keyword to hide the Parent's member field.

提交回复
热议问题