如何创建静态类的对象? [英] How to create object of static class ?

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

问题描述



在一次采访中,我被问到这样一个问题:当我在任何地方读到我们无法创建静态类的对象时,我们如何创建静态clsss的对象?请指导是否可以创建如何?

Hi,
In an interview I was asked this questions that how can we create an object of static clsss while I have read on everwhere that we can't create object of static class? Please guide if can create then how ?

推荐答案

MSDN:

一个类可以声明为static,表示它只包含静态成员。无法使用new关键字创建静态类的实例。

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.





检查静态类和静态类成员(C#编程指南) [ ^ ]。



此外:



Check Static Classes and Static Class Members (C# Programming Guide)[^].

Moreover:

Quote:



他们只包含静态成员。

它们无法实例化。

它们是密封的。

它们不能包含实例构造函数(C#编程指南)。


They only contain static members.
They cannot be instantiated.
They are sealed.
They cannot contain Instance Constructors (C# Programming Guide).

< br $>




--Amit




--Amit


是的,我们可以为静态类创建对象。但它确实没有意义,也不是必需的。



例如:



Yes we can create object for static classes. But it really does not make sense and is not required.

For eg:

class Sample
{
    public static void main(String args[])
    {
        Math m=new Math();
        System.out.println(m.sqrt(120)); //with help of object
        System.out.println(Math.sqrt(120)); //with out object
    }
}


首先监听,如果我们看到C#中非静态类和静态类之间的区别,那就是无法实例化静态类。这意味着您不使用new关键字来实例化该类的实例。相反,您将使用类名访问静态类的成员。基本上,静态类旨在成为一组方法的容器,这些方法仅使用提供的参数,并且不需要存储或检索它们唯一的数据。 .NET Framework中内置的静态类的一个示例是System.Math类。它包含执行各种数学运算的方法。



静态类就像一个只有静态成员且有私有构造函数的常规类。如果你的设计中有这样的类,最好使它成为一个静态类,以便使用编译器中的功能,这将确保无法创建或继承类的实例。通过这一步,它将使您的代码更加稳固。



有时在静态类和单例类之间存在混淆,其中只允许一个实例。分离它们的方法是理解静态类不应该有任何内部变量,每个方法应该独立运行,就像函数库一样。但是,单例类可以将内部值公开为属性,并允许存储和检索这些值。



如上所述,静态类无法实例化。相反,框架将在代码中引用类的第一个位置之前调用静态类的私有构造函数。这允许你将函数库无缝地融入你的程序中,所以如果你有这样的静态类...

public static class MyFunctions

{

公共静态字符串Test1(字符串myValue)

{

//代码

}



public static int Test2(int myValue)

{

//代码

}

}



...你可以这样称呼它......

MyFunctions.Test1(CurrentValue);

...无需使用new关键字创建对象的实例(无论如何都无法使用静态类)。此外,静态类将在创建它的应用程序域的生命周期内保留在内存中。



正如您在上面的示例中所看到的,静态类可以仅包含静态成员。如果您不记得将每个程序标记为静态,则程序将无法编译。此外,静态类是密封的,这意味着它们不能被继承,也不能从System.Object以外的任何类继承。虽然静态类不能具有实例构造函数,但它们可以具有私有的静态构造函数。当Framework创建对象的实例时,将调用此例程。



这里要记住的一件事是,如果静态类需要大量的初始化,那么它可能不适合静态类。在我看来,这些类适用于相关但基本上是独立的功能组。拥有一个实质性的初始化例程可能表明该类可能作为单例类或常规类更好地工作。在早期考虑这一点可以防止你不得不将静态类重新编码为其他东西。
Listen first, If we see the difference between a non-static and a static class in C# is simply that a static class cannot be instantiated. This means that you don’t use the new keyword to instantiate an instance of the class. Instead, you will access members of the static class by using the class name. Basically, a static class is intended to be a container for a set of methods that only work with supplied parameters and don’t have a need to store or retrieve data that is unique to them. An example of a static class that’s built into the .NET Framework is the System.Math class. It contains methods which perform various mathematical operations.

A static class is like a regular class that only has static members and has a private constructor. If you have a class like this in your design it’s best to make it a static class in order to use the features in the compiler which will insure that instances of the class cannot be created or inherited. By taking this step it will make your code more solid.

Sometimes there is confusion between a static class and a singleton class where only one instance is allowed. The way to separate them is to understand that the static class should not have any internal variables, each method should function independently, like a function library. However, a singleton class may expose internal values as properties and allow the storage and retrieval of these values.

As I mentioned above static classes cannot be instantiated. Instead, the Framework will call the private constructor for the static class prior to the first place the class is referenced in code. This allows you to blend in your function library seamlessly into your program so if you had a static class like this…
public static class MyFunctions
{
public static string Test1(string myValue)
{
// Code
}

public static int Test2(int myValue)
{
// Code
}
}

…you can call it like so…
MyFunctions.Test1(CurrentValue);
…without having to create an instance of the object using the new keyword (which you can’t do with a static class anyway). Also, a static class will remain in memory for the lifetime of the application domain where it is created.

As you can see in the example above, a static class can only contain static members. The program won’t compile if you don’t remember to mark each of them as static. Also, static classes are sealed, which means that they cannot be inherited and cannot inherit from any class other than System.Object. While a static class cannot have an instance constructor, they can have a private, static, constructor. This routine will be called when the Framework creates the instance of the object.

One thing to bear in mind here is that if your static class requires a lot of extensive initialization it may not be a good candidate for a static class. In my view, these classes are intended for groups of related, but essentially standalone, functions. Having a substantial initialization routine may indicate that the class might work better as a singleton class or as a regular class. Taking this into consideration early on can prevent you from having to recode a static class into something else.


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

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