C# OOPs Static Class

本文关键字:Class Static OOPs | 更新日期: 2023-09-27 17:55:07

您能告诉我以下两类有什么区别吗

public static class Product
{
    public static int AddData(int x, int y)
    {
        return x + y;
    }
}
public class Product
{
    public static int AddData(int x, int y)
    {
        return x + y;
    }
} 

因为我们可以在两个类中以相同的方式访问AddData方法

C# OOPs Static Class

  1. 在静态类中只能添加静态方法

  2. 你不能创建静态类的实例也就是说它不能被赋值给变量

  3. 在静态类中访问静态方法比在实例中访问静态方法要快。

  4. 静态类也不能被继承(这是没有意义的,因为它们不能有实例)