为什么使用静态类? [英] Why are static classes used?

查看:159
本文介绍了为什么使用静态类?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对静态类和静态方法的怀疑。从MSDN我了解,静态类和类成员被用于创建不需要创建类的实例来访问数据和功能。

I have doubts on static class and static methods. From MSDN I understood that "Static classes and class members are used to create data and functions that can be accessed without creating an instance of the class."

所以,如果我们不不想一类通过一个实例相关联,我们将使其为静态。这是唯一的优势在哪里?任何人都可以引导我,我们去的静态类的实时场景。

So if we don't want to associate a class over an instance , we will make it as static. Is that the only advantage? Can anyone guide me in which real time scenario we go for static class.

有的时候在课堂上(而不是静态的)我发现静态方法。什么优势/性能比较优势做静态的方法给出了在实际实例的方法。

Some time in classes(not static) I am finding static methods. What advantage/perfomance advantage do static methods give over instance methods in practical.

推荐答案

有关的实用工具类,他们都是伟大的。正如你所说,他们是类同的全局状态。因此,对于那些没有状态类,性能益类应该是静态的。

For utility classes they are great. As you mentioned, they are similiar to global state. So for classes which have no state, for performance benefits the class should be static.

在另一方面,静态类难以测试(如果它们包含状态)。多态性等面向对象的概念也失去了。

On the other hand, static classes are hard to test (if they contain state). Polymorphism and other OO concepts are also lost.

明智地使用。

这篇关于为什么使用静态类?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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