Some questions about abstract class with private, public and protected constructors

前端 未结 5 1324
北海茫月
北海茫月 2021-02-09 21:30

My first question: What is the difference between an protected and a public constructor in an abstract class?

My second questions: Does it make sense, if the abstract cl

5条回答
  •  余生分开走
    2021-02-09 22:14

    There doesn't seem to be much difference to me, the following code outputs

    Foo
    Bar

    public abstract class Foo
    {
        protected Foo() {
            Console.WriteLine ("Foo");
        }
    }
    
    public class Bar : Foo
    {
        public Bar() {
            Console.WriteLine ("Bar");
        }
    }
    
    void Main()
    {
        new Bar();
    }
    

    An abstract constructor can not be overriden if it's protected. Not sure If I answered your question :-)

提交回复
热议问题