Can you help me understand in a practical example the usage abstract classes vs interfaces?

前端 未结 9 609
孤城傲影
孤城傲影 2020-12-24 04:09

Can you give me an almost overly simplistic understanding of abstract class vs inheritance use and help me so I can truly understand the concept and how to implement? I have

9条回答
  •  生来不讨喜
    2020-12-24 04:56

    Another suggestion - (slightly off topic, but still related)

    I recommend, for learning purposes, to avoid automatic properties. It will help you understand what's happening if you implement them explicitly.

    For example, instead of doing:

    class SomeClass
    {
       public string MyProperty
       {
           get;
           set;
       }
    }
    

    Try implementing this yourself:

    class SomeClass
    {
        public string MyProperty
        {
            get
            {
                 return "MyValue"; // Probably a private field
            }
            set
            {
                 // myField = value; or something like that
            }
    }
    

    I mention this because it will help you in this specific case. Since you're using automatic properties, the compiler is "filling in the blanks" for you, and in your case, I think it's preventing you from getting some very useful compiler errors. When trying to understand how these concepts work, doing the work yourself usually makes it easier, not harder.

提交回复
热议问题