简体   繁体   English

为什么默认情况下不密封类?

[英]Why aren't classes sealed by default?

I was just wondering, since the sealed keyword's existence indicates that it's the class author's decision as to whether other classes are allowed to inherit from it, why aren't classes sealed by default, with some keyword to mark them explicitly as extensible?我只是想知道,由于Sealed关键字的存在表明是否允许其他类从它继承是类作者的决定,为什么默认情况下不密封类,用一些关键字将它们显式标记为可扩展?

I know it's somewhat different, but access modifiers work this way.我知道这有点不同,但访问修饰符是这样工作的。 With the default being restrictive and fuller access only being granted with the insertion of a keyword.默认情况下是限制性的,只有插入关键字才能授予更全面的访问权限。

There's a large chance that I haven't thought this through properly, though, so please be humane!不过,我很有可能没有正确地考虑过这一点,所以请保持人性化!

I'd say it was just a mistake.我会说这只是一个错误。 I know many people (including myself) who believe that classes should indeed be sealed by default.我知道很多人(包括我自己)相信类确实应该默认密封。 There are at least a couple of people in the C# design team in that camp.在那个阵营中,C# 设计团队中至少有几个人。 The pendulum has swung somewhat away from inheritance since C# was first designed.自从 C# 最初被设计以来,钟摆已经在某种程度上偏离了继承。 (It has its place, of course, but I find myself using it relatively rarely.) (当然,它有它的位置,但我发现自己很少使用它。)

For what it's worth, that's not the only mistake along the lines of being too close to Java: personally I'd rather Equals and GetHashCode weren't in object, and that you needed specific Monitor instances for locking too...就其价值而言,这并不是过于接近 Java 的唯一错误:我个人宁愿 Equals 和 GetHashCode 不在对象中,而且您也需要特定的 Monitor 实例来锁定......

In my opinion there should be no default syntax, that way you always write explicitly what you want.在我看来,不应该有默认语法,那样你总是明确地写出你想要的。 This forces the coder to understand/think more.这迫使编码人员理解/思考更多。

If you want a class to be inheritable then you write如果你想要一个类是可继承的,那么你写

public extensible class MyClass

otherwise除此以外

public sealed class MyClass

BTW I think the same should go with access modifiers, disallow default access modifiers.顺便说一句,我认为访问修饰符也应该如此,禁止默认访问修饰符。

继承是 OO 的基本原则,因此可以说,默认情况下禁止继承是不直观的。

You could probably make just as many arguments in favor of sealed-by-default as you could against it.您可能会提出尽可能多的支持默认密封的论据。 If it were the other way around, someone would be posting the opposite question.如果反过来,有人会发布相反的问题。

I can't recall having heard a rationale for the decision to have classes not sealed by default.我不记得听说过默认情况下不密封类的决定的理由。 However, there are certainly quite a few people who believe that C# should have been spec'ed to have sealed be the default:但是,肯定有不少人认为 C# 应该被指定为默认值:

http://codebetter.com/blogs/patricksmacchia/archive/2008/01/05/rambling-on-the-sealed-keyword.aspx http://codebetter.com/blogs/patricksmacchia/archive/2008/01/05/rambling-on-the-sealed-keyword.aspx

sealed classes prevent inheritance and therefore are an OO abombination.密封类防止继承,因此是面向对象的组合。 see this rant for details ;-)有关详细信息,请参阅此咆哮;-)

Merely deriving from an unsealed class doesn't change the class's behavior.仅仅从未密封的类派生不会改变类的行为。 The worst that can happen is that a new version of the base class will add a member with the same name as the deriving class (in which case there will just be a compiler warning saying you should use the new or override modifier) or the base class is sealed (which is a design no-no if the class has already been released into the wild).可能发生的最坏情况是基类的新版本将添加一个与派生类同名的成员(在这种情况下,只会有一个编译器警告说您应该使用newoverride修饰符)或基类类是密封的(如果类已经被释放到野外,这是一个设计禁忌)。 Arbitrary sublassing still complies with the Liskov Substitution Principle .任意 sublassing 仍然符合Liskov 替换原则

The reason that members are not overridable by default in C# is that because overriding a method can change the base class's behaviour in a way that the base class's author didn't anticipate.在 C# 中默认成员不可重写的原因是因为重写方法可以以基类的作者没有预料到的方式更改基类的行为。 By making it explicitly abstract or virtual, it's saying that the author is aware that that it can change or is otherwise beyond their control and the author should have taken this into account.通过将其明确抽象或虚拟化,表示作者意识到它可以改变或超出他们的控制范围,作者应该考虑到这一点。

80% of the features of Word go unused. Word 80% 的功能都没有使用。 80% of classes don't get inherited from. 80% 的类没有被继承。 In both cases, once in a while, someone comes along and wants to use or reuse a feature.在这两种情况下,偶尔会有人出现并想要使用或重用某个功能。 Why should the original designer prohibit reuse?为什么原设计者要禁止重用? Let the reuser decide what they want to reuse.让重用者决定他们想要重用什么。

For the same reason why objects are not private by default出于同样的原因,默认情况下对象不是私有的

or或者

to be consistent with the object analogue, which is objects are not private by default与对象模拟一致,默认情况下对象不是私有的

Just guessing, coz at the end of the day it's a language's design decision and what the creators say is the canon material.只是猜测,因为归根结底这是一种语言的设计决定,而创作者所说的是经典材料。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM