密封类 [英] Sealing a class

查看:144
本文介绍了密封类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

密封类是不是我在过去采取在意,但是我发现自己想知道什么是最好的做法。如果你知道一个类就不会或不应该从你会封其为防治措施的问题衍生刚刚离开密封的关键字出来明知某人的机会试图能从中得到很渺茫。



我猜我问的是你应该封不打算继承,就像好的做法呢?


的问题的所有类< DIV CLASS =h2_lin>解决方案

您不要随意封都没有,在任何特定时间的所有类,打算从继承。



相反,你知道密封类绝对不能从继承并离开他人开放。如果在某个时间点后,可以作出决定,这些类绝对不能继承,你可以将它们密封即可。


Sealing a class is not something I have taken much notice of in the past however I find myself wondering what is the best practice. If you know a class would not or should not be derived from would you seal it as a matter of precaution of just leave the sealed keyword out knowing that the chances of someone trying to derive from it are slim.

I guess what I am asking is should you seal all classes which are not intended for inheritance just as a matter of good practice?

解决方案

You should not arbitrarily seal all classes that are not, at any given time, meant to be inherited from.

Instead, seal classes that you know should NEVER be inherited from and leave the others open. If, at some later point in time, a decision can be made that those classes should NEVER be inherited from you can seal them then.

这篇关于密封类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆