What are the advantages of non-static class over s

2020-06-18 02:48发布

What are the advantages of non-static class over static class?

static class need not be instanciated. so we can directly use ClassName.MemberName, so then whats the use of nonstatic class

标签: c# .net
5条回答
成全新的幸福
2楼-- · 2020-06-18 03:25

Think of a class as a building blueprint and instances of that class (objects) as buildings built according to the design in the blueprint.

Given one blueprint, you can create many buildings. Each of these buildings has the same structure, but each is independent - one could have its door open while another has its door closed.

A static class is like a blueprint that can never be used to build any houses. You can still do things like tear it up or spill coffee on it, but you could never open the door on the blueprint since it doesn't have an actual door, only the design for a door.

Another way of describing this situation is by considering 'state'. A building (object) has some state (e.g. whether the door is open or not) and the state of each building can be different (e.g. whether it's own door is open). A blueprint (class) can also have (static) state (e.g. whether coffee has been spilled on it or not) but this is different and separate to the state associated with the buildings built according to the blueprint.

public class House {
    private static boolean isCoffeeSpilt;

    private boolean isDoorOpen;

    public static void spillCoffee() {
        House.isCoffeeSpilt = true;
    }

    public void openDoor() {
        isDoorOpen = true;
    }
}

public static class Program {
    public static void main(String[] args) {
        House redHouse = new House();   // 1
        House blueHouse = new House();  // 2

        redHouse.openDoor();            // 3

        House.spillCoffee();            // 4
    }
}

In the example above, lines 1, 2, and 3 would not be possible if House was a static class. Note that openDoor is invoked on an instance of House while spillCoffee is invoked on the class itself. Note also that at the point of line 4 the red house's door is open but the blue house's door is still closed.

查看更多
一纸荒年 Trace。
3楼-- · 2020-06-18 03:36

An advantage of non-static class is that static classes are horrible to unit test - they can't easily be mocked, and they don't support interfaces.

查看更多
祖国的老花朵
4楼-- · 2020-06-18 03:39

Say you need to store information and operations that encapsulate an employee.

class Employee
{  
    string Name {get; set;}
    double HourlyRate {get; set;}
    double CalculatePay(int hoursWorked) { . . . }
}

Now, lets say your program needs to compare two Employees at the same time?

Exactly! With a non static employee class you can instantiate two (or more) instances of the Employee class, each of these objects represents a different Employee.

You could not do this of Employee was static.

e.g.

void CompareEmployees(Employee empA, Employee empB)
{
    if(empA.HourlyRate < empB.HourlyRate)
        Console.WriteLine("Employee {0} has a higher rate than {1}", empA.Name, empB.Name);

    else if(empA.HourlyRate > empB.HourlyRate)
        Console.WriteLine("Employee {0} has a higher rate {1}", empb.Name, empA.Name);
    else
        Console.WriteLine("{0} and {1} have the same rate", empb.Name, empA.Name);
}
查看更多
干净又极端
5楼-- · 2020-06-18 03:44

A static class is basically the same as a non-static class, but there is one difference: a static class cannot be instantiated. In other words, you cannot use the new keyword to create a variable of the class type. Because there is no instance variable, you access the members of a static class by using the class name itself

A static class has only one instance of the class itself so you cannot create multiple instance of a static class.

looking at your question you cannot set two different values to the below property if the class is static because there will be only one instance of ClassName in memory

ClassName.MemberName

more info at

http://msdn.microsoft.com/en-us/library/79b3xss3.aspx

查看更多
时光不老,我们不散
6楼-- · 2020-06-18 03:44

A static class cannot be instanced(technically it is, but only once), and the static constructor is "usually" called on the first access to the class, which can be difficult to track and coordinate. Static classes are good if you want one class that just gathers some utility methods. Math in the .net framework is a good example. Also they are usefull for extension methods. Besides that i don't use them.

If you need a single instance, and don't want to be bound to the constraints a static class has, the Singleton pattern is very useful. A nice article about the difference between Singletons and static classes.

And of course, a non static class must be instanced, stored in a variable and can be instanced multiple times.

Example:

A static class, just helper methods for loading a file:

static class TextureHelper
{
   public static byte[] LoadFromFile(string aFileName);
}

a Singleton that is the main point to create textures.

public sealed class TextureManager
{
   public static TextureManager Instance{get;set;}

   public Texture CreateTexture();
}

an actual texture, which i might have many of in my application.

public class Texture
{

}
查看更多
登录 后发表回答