实例类和静态类之间的区别 [英] Difference between instance classes and static classes

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

问题描述





我用静态方法创建了一个普通类,用静态方法创建了静态类。在这两种情况下,我都可以通过提供CLASSNAME来访问静态方法.staticmethodname而不是instance.staticmethodname。那么使用静态类有什么好处?

请帮帮我。





谢谢,

Roshma

Hi,

Me created a normal class with static methods and also Static classes with static methods.In both cases,i can access static methods by giving "CLASSNAME.staticmethodname" instead of "instance.staticmethodname".So what is the benefit of using static classes?
Please help me.


Thanks,
Roshma

推荐答案

MyObject.staticMethod();         //Simply refers to the class's static code

MyObject obj = new MyObject();   //Create an instance
obj.nonstaticMethod();           //Refer to the instance's class's code



在更深层次上,当编译器将类放在一起时,它包含几个指向方法的指针。当执行这些方法时,它遵循指针并执行远端的代码。如果实例化了类,则创建的对象包含指向虚方法表的指针,该指针指向要在继承层次结构中为该特定类调用的方法。但是,如果方法是静态的,则不需要虚方法表:对该方法的所有调用都会到达内存中完全相同的位置以执行完全相同的代码。因此,在高性能系统中,如果不依赖实例变量,最好使用静态方法。


On a deeper level, when the compiler puts a class together, it contains several pointers to methods. When those methods are executed it follows the pointers and executes the code at the far end. If a class is instantiated, the created object contains a pointer to the "virtual method table", which points to the methods to be called for that particular class in the inheritance hierarchy. However, if the method is static, no "virtual method table" is needed: all calls to that method go to the exact same place in memory to execute the exact same code. For that reason, in high-performance systems it's better to use a static method if you are not reliant on instance variables.


静态类与非静态类基本相同,但是有一个区别:静态类无法实例化 - 你不能使用 new 关键字来创建类类型的实例,你不能使用该类变量声明左侧的名称,并且您无法创建返回静态类的方法,因为您无法返回值!



因为有没有实例变量,你只能通过使用类名本身来访问静态类的成员,如你所见。



但是...有三个重要的关于静态类的其他事情:

1)你不能声明非静态类成员。如果您尝试,将收到编译错误。这意味着用户需要使用类名而不是类实例更为明显。

2)你只能 在静态类中声明扩展方法。

3)静态类有一个静态构造函数,当引用该类的程序被加载时,它被执行(一次,只有一次) - 它是保证加载并初始化其字段,并在第一次引用类之前调用​​其静态构造函数,并且静态类在程序所在的应用程序域的生命周期内保留在内存中。
A static class is basically the same as a non-static class, but there is one difference: a static class cannot be instantiated - you can't use the new keyword to create a instance of the class type, you can't use the class name on the left of a variable declaration, and you can't create a method that returns a static class because you can't return a value!

Because there is no instance variable, you can only access the members of a static class by using the class name itself, as you have seen.

But...there are three important other things about static classes:
1) You cannot declare non-static class members. If you try, you will get a compilation error. This means that it is a lot more obvious to a user that he needs to use the class name, rather than a class instance.
2) You can only declare Extension methods within static classes.
3) Static classes have a static constructor, which is executed (once, and once only) when the program that references the class is loaded - it is guaranteed to be loaded and to have its fields initialized and its static constructor called before the class is referenced for the first time and a static class remains in memory for the lifetime of the application domain in which your program resides.


使类静态只会阻止人们尝试创建它的实例。如果你的所有班级都是静态成员,最好让班级本身为静态。





如果一个班级被宣布为static然后变量和方法应强制声明为static。



类可以声明为static,表示它只包含静态成员。使用new关键字无法创建静态类的实例。当加载包含类的程序或命名空间时,.NET Framework公共语言运行库(CLR)会自动加载静态类。



使用静态类来包含方法与特定对象无关的。例如,创建一组不对实例数据起作用且与代码中的特定对象无关的方法是一种常见的要求。您可以使用静态类来保存这些方法。



静态类的主要功能是:



他们只包含静态成员。

他们无法实例化。

他们是密封的。

他们不能包含实例构造函数或只是构造函数我们知道它们与对象相关联,并在创建对象时对数据进行操作。
Making a class static just prevents people from trying to make an instance of it. If all your class has are static members it is a good practice to make the class itself static.


If a class is declared as static then the variables and methods should compulsorily be declared as static.

A class can be declared static, indicating that it contains only static members. It is not possible to create instances of a static class using the new keyword. Static classes are loaded automatically by the .NET Framework common language runtime (CLR) when the program or namespace containing the class is loaded.

Use a static class to contain methods that are not associated with a particular object. For example, it is a common requirement to create a set of methods that do not act on instance data and are not associated to a specific object in your code. You could use a static class to hold those methods.

The main features of a static class are:

They only contain static members.
They cannot be instantiated.
They are sealed.
They cannot contain Instance Constructors or simply constructors as we know that they are associated with objects and operates on data when an object is created.


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

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