English 中文(简体)
我是否应当建议按违约分类密封等级?
原标题:Should I recommend sealing classes by default?

在我工作的一个大型项目中,我正在考虑建议其他方案者,如果他们不考虑他们的班级应如何分级的话,就永远取消班级。 经常,经验较差的方案制定者从未考虑过这一点。

我感到奇怪的是,在贾瓦和C#类学校,没有收齐/没有定点。 我认为,加固班级将大大提高法典的可读性。

通知说,如果出现需要分类的罕见情况,这是内部法典,我们总是能够改变。

你们的经验是什么? 我对这一想法感到相当抵制。 难道不能把他们的zy放在“条码”的类型上吗?

最佳回答

Okay,如同许多其他人一样,在......中被权衡。

是的,我认为,完全有理由建议课堂按违约加以密封。

这与Schobalch在他的excellent书上的建议一致。 有效 Java,第2版:

设计继承或禁止继承。

Designing for inheritance is hard, and can make your implementation less flexible, especially if you have virtual methods, one of which calls the other. Maybe they re overloads, maybe they re not. The fact that one calls the other must be documented otherwise you can t override either method safely - you don t know when it ll be called, or whether you re safe to call the other method without risking a stack overflow.

现在,如果你后来想改变哪一种方法,你可以 t——你可能会打破子类。 因此,以“灵活性”的名义,你实际上使执行less具有灵活性,并且必须更加仔细地记录执行细节。 这对我来说是一种伟大的想法。

接下来是不可改变的——我喜欢不可改变的类型。 我认为,他们比变幻不定的类型更容易说明。 这是Date 和。 但是,一个不受干扰的班子永远不会是已知<>>/em>的可变。 如果我接受“<代码>的参数”。 Foo,我可以依靠在<<>>上申报的 财产,但不能随时间而改变,但我不能依靠该物体本身而不作修改——在子类别中可能有一个变性财产。 如果用某种虚拟方法使用该财产,则我会提供帮助。 顺差到可变性的许多好处。 (当然,若达时代拥有大量继承等级——通常说“子级”是不可改变的。) <代码>Chronology的大型继承等级使得在进入C#时难以理解。

最后,遗产的过度使用方面。 在可行的情况下,我个人赞成组成而不是继承。 我爱聚吗? 我使用执行遗产——但我的经验很少。 穿制服的班级避免了从哪里组成更合适。

EDIT:我还想在以来,Lippert s blog 员额说明为何如此多的框架班级被封。 我祝愿网提供interface<>。 我们可以努力寻找可检测性,但这一请求略有不同。

问题回答

我认为,作出建筑设计决定,向其他开发商(包括未来维修开发商)传达很重要的内容。

海运班级指出,执行不应优先于执行。 它指出,这一类人不应冒名。 有很好的理由加以密封。

如果你采取不同寻常的做法来封存所有东西(这是不寻常的),那么,你的设计决定现在就传达了实际上无关重要的东西,例如,这一类别为t inook,将由原始/作者开发商继承。

但是,那么,你会如何向其他开发商通报:should不会因某种原因而继承? 确实,你可以失败。 你 st。

此外,加固某类杂草也提高了可读性。 我看不到这一点。 如果继承是欧佩组织发展中的一个问题,那么我们的问题就会大得多。

我认为,我是一位经验合理的方案家,如果我没有学到其他东西的话,我就认为在预测未来时我非常糟糕。

典型的封锁并非是困难的,我只是想ir开一条路(我可以说!)的 develop子,他发现,一个问题可以轻松地通过遗产解决。

我也没有任何想法,说明如何使某一类别更加可读。 你们是否试图强迫人们选择组成而不是继承?

<><>>>>> Jeffrey Richter

There are three reasons why a sealed class is better than an unsealed class:

  • Versioning: When a class is originally sealed, it can change to unsealed in the future without breaking compatibility. However, once a class is unsealed, you can never change it to sealed in the future as this would break all derived classes. In addition, if the unsealed class defines any unsealed virtual methods, ordering of the virtual method calls must be maintained with new versions or there is the potential of breaking derived types in the future.
  • Performance: As discussed in the previous section, calling a virtual method doesn’t perform as well as calling a nonvirtual method because the CLR must look up the type of the object at runtime in order to determine which type defines the method to call. However, if the JIT compiler sees a call to a virtual method using a sealed type, the JIT compiler can produce more efficient code by calling the method nonvirtually. It can do this because it knows there can’t possibly be a derived class if the class is sealed.
  • Security: and predictability A class must protect its own state and not allow itself to ever become corrupted. When a class is unsealed, a derived class can access and manipulate the base class’s state if any data fields or methods that internally manipulate fields are accessible and not private. In addition, a virtual method can be overridden by a derived class, and the derived class can decide whether to call the base class’s implementation. By making a method, property, or event virtual, the base class is giving up some control over its behavior and its state. Unless carefully thought out, this can cause the object to behave unpredictably, and it opens up potential security holes.

在继承某类财产方面没有任何错误。

只有当有充分理由永远不能继承时,你才应穿上阶级。

此外,如果你封住所有这些,它只会降低可维持性。 每次有人想要从你的一个班子继承,他都会看到这门课被封,然后他要么去除封条(用手脚的密码给我),要么更糟:给自己造成本班执行不力。

然后,你执行两件相同物品,可能比另一件更糟,另外还有两部法典。

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

坦率地说,我认为,在c#中,没有穿附的班级是休养的,而其他拖欠款是如何用语言工作的。

By default, classes are internal. By default fields are private. By default members are private.

似乎有一种趋势表明,通过违约获得治疗的机会最少。 有理由说,<代码>unsealed 关键词应当从c#中删除,而不是sealed

个人情况是,一年级按停课。 在多数学员撰写某门课时,他并没有在设计时顾及分门别类以及随之而产生的所有复杂情况。 指定今后分级的做法应当是有意识的行为,因此,我必须明确表示。

“......考虑应如何分门别类。

在过去几年里,我至少看到了数十次,我发现自己为一些公开的消息来源小组或另一个小组在受保护和私营混合体中的空档,因此不可能简单地延长一个班级,而不必复制整个家长阶层的来源。 (在大多数情况下,超越特定方法需要接触私人成员。)

一个实例是,一个几乎是我所希望的“联合起来”标签。 我需要推翻一件小事。 Nope, sorry,我不得不完全复制父母的消息来源。

我只有穿制服班,如果我正在从事我打算分发的可再利用部分的工作,我不想让终端用户从中继承,或者如果我知道,我不想让小组的另一名开发商继承。 然而,通常会有一些理由。

Just because a class isn t being inherited from, I don t think it should automatically be marked sealed. Also, it annoys me to no end when I want to do something tricky in .NET, but then realize MS marks tons of their classes sealed.

This is a very opinionated question that s likely to garner some very opinionated answers ;-)

That said, in my opinion, I strongly prefer NOT making my classes sealed/final, particularly at the beginning. Doing this makes it very difficult to infer the intended extensibility points, and it s nearly impossible to get them right at the beginning. IMHO, overuse of encapsulation is worse than overuse of polymorphism.

您的家,您的统治。

You can also have the complementary rule instead: a class that can be subclassed must be annotated; nobody should subclass a class that s not annotated so. This rule is not harder to follow than your rule.

The main purpose of a sealed class to take away the inheritance feature from the user so they cannot derive a class from a sealed class.Are you sure you want to do that. Or do you want to start having all classes as sealed and then when you need to make it inheritable you will change it .. Well that might be ok when every thing is in house and in one team but incase other teams in future use your dlls it will be not possible to recompile whole source code everytime a class needs to be unsealed .... I wont recommend this but thats just my opinion

我不想这样想。 Java和c#制成OOP语言。 这些语言的设计方式是课堂能够有父母或子女。 这就是说。

一些人说,我们应永远从限制最大的变压器(私人、受保护......)开始,只有在你对外使用时,才让你们的成员公开。 在我看来,这些人是 la,不想在项目开始时想好设计。

My answer is: Design your apps in a good way now. Set your class to seal when it needs to be sealed and private when it needs to be private. Don t make them sealed by default.

我认为,在我的经验中,封闭的/最后的班级实际上非常少见;我当然不会建议所有班级都按规定加以密封/最后定级。 这种说明对法典状况作了某些说明(即它已经完备),在发展时期不一定总是如此。

我还补充说,要让某类人不受干扰,就需要作出更多的设计/测试努力,以确保暴露的行为得到明确界定和测试;重型单位检测是海事组织的关键,目的是对“密封”的支持者似乎希望的那类行为达到一定程度的信任。 但海事组织认为,这一努力的增加直接转化为高度信任和更高的质量标准。





相关问题
Spring Properties File

Hi have this j2ee web application developed using spring framework. I have a problem with rendering mnessages in nihongo characters from the properties file. I tried converting the file to ascii using ...

Logging a global ID in multiple components

I have a system which contains multiple applications connected together using JMS and Spring Integration. Messages get sent along a chain of applications. [App A] -> [App B] -> [App C] We set a ...

Java Library Size

If I m given two Java Libraries in Jar format, 1 having no bells and whistles, and the other having lots of them that will mostly go unused.... my question is: How will the larger, mostly unused ...

How to get the Array Class for a given Class in Java?

I have a Class variable that holds a certain type and I need to get a variable that holds the corresponding array class. The best I could come up with is this: Class arrayOfFooClass = java.lang....

SQLite , Derby vs file system

I m working on a Java desktop application that reads and writes from/to different files. I think a better solution would be to replace the file system by a SQLite database. How hard is it to migrate ...