Not sure when to use an abstract property and when not

后端 未结 5 1140
野性不改
野性不改 2020-12-04 12:52

I\'m not really sure what looks better or when do I really use in abstract classes and properties, or when to use non abstract properties. I\'ll try to make a simple example

5条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2020-12-04 13:57

    An abstract property would be used where you want the class to always expose the property, but where you can't pin down the implemetation of that property - leaving it up to/forcing the inheriting class to do so.

    There's an example here, where the abstract class is named Shape, and it exposes an abstract Area property. You can't implement the Area property in the base class, as the formula for area will change for each type of shape. All shapes have an area (of some sort), so all shapes should expose the property.

    Your implementation itself looks just fine. Was trying to think of a sensible example of an abstract property for a Human, but couldn't think of anything reasonable.

提交回复
热议问题