Protected readonly field vs protected property

后端 未结 7 1729
广开言路
广开言路 2021-01-01 17:07

I have an abstract class and I\'d like to initialize a readonly field in its protected constructor. I\'d like this readonly field to be available in derived classes.

7条回答
  •  离开以前
    2021-01-01 17:35

    Derived classes are still 'users' of the original code; fields should be encapsulated from them too.

    You should think of base classes as safe and extendable APIs, rather than just classes which expose their internals. Keep the fields private - apart from anything, it allows the base class to change how that property's value is generated :)

提交回复
热议问题