Accessing a static property of a child in a parent method

前端 未结 9 1242
孤城傲影
孤城傲影 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:45

    In one response you seem to indicate that the static field is constant. If that is the case, then this should work for you.

    class Parent
    {
        protected virtual string MyField() { return "ParentField"; }
    
        public virtual string DoSomething()
        {
            return MyField();
        }
    }
    
    class Child : Parent
    {
        protected override string MyField() { return "ChildField"; }
    } 
    

提交回复
热议问题