Should I recommend sealing classes by default?

后端 未结 13 1020
青春惊慌失措
青春惊慌失措 2021-01-30 01:57

In a big project I work for, I am considering recommending other programmers to always seal their classes if they haven\'t considered how their classes should be subclassed. Oft

13条回答
  •  一个人的身影
    2021-01-30 02:44

    There shouldn't be anything wrong in inheriting from a class.

    You should seal a class only when theres a good reason why it should never be inherited.

    Besides, if you seal them all, it will only decrease maintainability. Every time someone will want to inherit from one of your classes, he will see it is sealed, then he'll either remove the seal (mess with code he shouldn't have to mess with) or worse: create a poor implementation of your class for himself.

    Then you'll have 2 implementations of the same thing, one probably worse than the other, and 2 pieces of code to maintain.

    Better just keep it unsealed. No harm in it being unsealed.

自定义标题
段落格式
字体
字号
代码语言
提交回复
热议问题