我应该标记所有方法的虚拟? [英] Should I mark all methods virtual?

查看:163
本文介绍了我应该标记所有方法的虚拟?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Java中,你可以标记方法,最终使它的无法的覆盖。



在C#中,你必须标记方法,虚拟到使它的可能的覆盖。



这是否意味着,在C#中,你应该标记所有方法的虚拟(除非你不几的人要覆盖),因为很有可能你没有用什么方式你的类可以继承知道吗?


解决方案

在C#中,你必须标记方法,虚拟,使其可以覆盖。这是否意味着,在C#中,你应该标示所有虚拟方法(除非你不想被重写一些的),因为很有可能你没有用什么方式你的类可以继承知道吗?




没有。如果语言设计者认为虚拟应该是默认和这本来是默认



Overridablility是的功能的,像所有的功能它的费用的。一个覆写投放方法的成本是相当可观的:有很大的设计,实施和测试成本,特别是如果有任何敏感到类;虚方法是引入未经测试的第三方代码到系统的方式,并具有安全性的影响。



如果你不知道你是怎么打算的类继承随后的不发表您的类的原因的你还没说完呢设计它的你的可扩展模型是绝对的东西,你应该提前知道时间。它应该深刻影响您的设计和测试策略。



我主张所有的类是密封的所有方法的非虚的,直到你有一个真实世界以客户为中心的理由开封或进行虚拟的方法。



基本上,你的问题是我是无知的我的客户打算如何消耗我的课,我应该因此使其任意扩展的?没有;你应该的成为知识渊博的!你不会问我不知道我的客户将如何使用我的课,所以我应该让我的所有属性的读写?我应该委托类型的我的所有方法读写属性,使我的用户可以用自己的实现来替代任何方法?没有,直到你有没有做任何的那些东西的证据的一个用户实际需要的能力!花费您宝贵的时间设计,测试和用户实际想要和需要,并从知识的位置,这样做实现的功能。


In Java you can mark method as final to make it impossible to override.

In C# you have to mark method as virtual to make it possible to override.

Does it mean that in C# you should mark all methods virtual (except a few ones that you don't want to be overridden), since most likely you don't know in what way your class can be inherited?

解决方案

In C# you have to mark method as virtual to make it possible to override. Does it mean that in C# you should mark all methods virtual (except a few ones that you don't want to be overridden), since most likely you don't know in what way your class can be inherited?

No. If the language designers thought that virtual should have been the default then it would have been the default.

Overridablility is a feature, and like all features it has costs. The costs of an overrideable method are considerable: there are big design, implementation and testing costs, particularly if there is any "sensitivity" to the class; virtual methods are ways of introducing untested third-party code into a system and that has a security impact.

If you don't know how you intend your class to be inherited then don't publish your class because you haven't finished designing it yet. Your extensibility model is definitely something you should know ahead of time; it should deeply influence your design and testing strategy.

I advocate that all classes be sealed and all methods be non-virtual until you have a real-world customer-focussed reason to unseal or to make a method virtual.

Basically your question is "I am ignorant of how my customers intend to consume my class; should I therefore make it arbitrarily extensible?" No; you should become knowledgable! You wouldn't ask "I don't know how my customers are going to use my class, so should I make all my properties read-write? And should I make all my methods read-write properties of delegate type so that my users can replace any method with their own implementation?" No, don't do any of those things until you have evidence that a user actually needs that capability! Spend your valuable time designing, testing and implementing features that users actually want and need, and do so from a position of knowledge.

这篇关于我应该标记所有方法的虚拟?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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