静态与创建类的实例-Winform [英] static vs create an instance of the class - winform

查看:135
本文介绍了静态与创建类的实例-Winform的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


你能告诉我吗
1-何时在另一个上使用
2-一个相对于另一个的优缺点
3-何时创建静态类?什么时候调用该类或何时编译该程序?

Hi,
Can you please let me know
1- when to use one over the other
2- Advantages/disadvantages of one over the other
3- When does the static class get created? when the class is called or when the program is compiled?

Thanks

推荐答案

在静态中,它们仅存在一个对象.因此,同一对象将在程序中共享.只要您对其进行修改,就会对所有人生效.
在实例类型中,值将是唯一的.它将不会共享给所有人.当您更改一个对象值时,不会影响其他对象.

当类是静态的时,您可以直接调用成员变量和函数,而无需创建对象.但是在其他情况下需要创建一个对象,这种方式提供了安全性.

当没有理由像.net Framework Math Class这样的实例时,将使用静态类.

数学类是静态的,因为没有充分的理由要拥有该类的对象然后维护该对象.


请参考:静态类和静态类成员(C#编程指南).
In static ,their exists only one object.So same object will share in program. Whenever you modify it then will effect to all.
In instance type, value will be unique.it will not share to all. When you change one object value will not effect to other.

When a class is static ,you can call member variable and function directly with out create object. But in other case need to create an object, this way provide security.

Static Classes are used when there is no reason to have instances like in .net Framework Math Class.

Math class is netural to be static becuase there is no good reason to have a object of this class and then maintain the object.


Please refer: Static Classes and Static Class Members (C# Programming Guide).


这篇关于静态与创建类的实例-Winform的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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