【问题标题】:regarding concept of anything static ...like method, member etc关于任何静态的概念......如方法、成员等
【发布时间】:2011-06-03 22:13:21
【问题描述】:

我有一个疑问......

1)我们可以从任何非静态方法调用和访问任何静态方法或变量,但从静态方法我们不能访问任何非静态方法或变量.....为什么。

2) 但我们可以从静态方法创建任何类的实例。

请告诉我更好地回答我的问题的原因。

class Class1
{
    static int x=0;
    int w = 0;
    private static Class2 test()
    {
        w = 88; // give error because w is not a static member.
        test1(); // give error because test1() is not a static function.
        Class2 z = new Class2(); // here i am creating instance of class2
        return z;
    }

    private int test1()
    {
        x = 9;
        return x;
    }
}

class Class2
{

}

谢谢

【问题讨论】:

    标签: c# oop


    【解决方案1】:

    查看关于静态类的 MSDN 文章和关于静态类的 basic tutorial

    在回答您的直接问题时,静态方法不是实例化对象 (this) 的一部分,因此无法访问任何需要实例化对象的方法/字段。

    在您的示例中,test1() 可以很容易地在字段w 上运行。在这种情况下,外部调用者可以调用Class1.test(),而无需创建 Class1 的任何字段。如果 Class1 已实例化,则其字段已创建,因此可以对其进行操作。

    我发现这个Stackoverflow Post 值得一读,了解更多关于静态的背景知识。

    【讨论】:

      【解决方案2】:

      1)我们可以从任何非静态方法调用和访问任何静态方法或变量,但从静态方法我们不能访问任何非静态方法或变量.....为什么。

      您无法通过静态方法调用访问非静态变量或方法的原因是它们尚未创建,更重要的是它们不存在于内存中。静态方法不会调用它所属的非静态类的构造函数。

      2) 但我们可以从静态方法创建任何类的实例。

      在静态方法中声明和实例化的任何东西仍在分配内存(记住范围),因此它们是可用的,但只能在该静态方法中使用。例如,您不能从静态方法中分配非静态类的成员。

      此链接可能有助于更详细地解释它。

      Static keyword demystified

      【讨论】:

        【解决方案3】:

        要了解您首先必须了解class 和实际object 之间的区别的原因。 我可以定义如下的类:

        public class Car
        {
          public string Brand {get; set;}
        }
        

        我要告诉编译器的是,如果我在哪里创建这样的东西,Car 会是什么样子。此时没有创建Car,但计算机了解汽车是什么。我们已经给它一个蓝图。这称为class

        接下来我可以去实际制造一辆汽车:

        Car myCar = new Car();
        myCar.Brand = "Ford";
        Car otherCar = new Car();
        otherCar.Brand = "BMW";
        

        现在myCar 包含一个实际的object,我告诉计算机它是一辆车。我还在otherCar 中创建了另一辆车。这些都是单独的对象。并且都有属于该对象的自己的Name

        所以现在我们有 2 个对象,但仍然只有 1 个类。

        现在有时可能需要propertiesmethodsCar 概念有一些联系,但不需要特定的Car 来工作。在 C# 中,我们可以使用 static 关键字定义此类成员。

        我们可以将以下method 添加到我们的类中:

        public static Car GetFirstCarEverBuild();
        

        要真正找到第一辆制造的汽车,我们不需要访问特定的汽车。我们会尝试在这里找到一辆车,但我们不会从一开始。所以我们将其标记为static。这也意味着我们必须访问汽车的Brand,因为我们没有object 可以从中获取它。这种情况甚至可能是根本没有创建Car,在这种情况下,该方法只能返回Null。从来没有一辆车存在过,你也不需要一辆车来发现它。

        静态字段和属性也是如此:

        public static Car firstCar;
        

        此字段只能存储 1 辆汽车和 1 辆。无论您制造多少辆汽车。它的价值与类而不是对象相关联。所以如果myCar 会在这个字段中存储一个值,那么otherCar 可以读取这个相同的值。

        考虑下面的例子来总结一下:

        public class Car
        {
          public static Car firstCar;
        
          public Car()
          {
            if (firstCar == null)
              firstCar = this;
          }
        
          public string Brand {get; set;}
        
          public static Car GetFirstCarEverBuild()
          {
            return firstCar;
          }
        }
        

        【讨论】:

          【解决方案4】:

          这与您不能在空对象上调用方法的原因相同 - 非静态方法/字段/属性需要调用类型的实例。

          【讨论】:

            【解决方案5】:

            想象一下你有这个课程:

            Class Product {
              static int getTotalWeightOfAllProducts()
              int getWeight()
            }
            

            您从不对从静态方法调用实例方法感兴趣的原因是它没有意义。 假设您有 2 种产品。

            电脑和 Xbox。 像这样

            var xbox = new Product()
            var computer = new Product()
            xbox.getWeight() // would return the xbox weight 
            computer.getWeight() // would return the computer weight 
            
            // But calling getWeight like this doesn't make sense
            Product.getWeight() //because weight is product specific thing to do
            
            //However getting all products total weight would make sense to return through a static call like this:
            Product.getTotalWeightOfAllProducts() // because "getting all products total weight" isn't a product specific thing to do. Therefore it should reside in a static method.
            

            基本上,实例方法是“产品”特定的行为,而静态方法是共享的“产品”功能。不知道这是否有意义。

            getTotalWeightOfAllProducts 甚至不知道 xboxcomputer 实例存在,因此从 getTotalWeightOfAllProducts 中调用 getWeight() 没有意义。因为即使有可能,它会返回什么? xbox的重量还是电脑的重量?

            【讨论】:

              【解决方案6】:

              您必须对静态和实例成员有基本的了解。

              【讨论】:

              • 这并不能真正帮助 OP,因为这正是他们所寻求的。
              猜你喜欢
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 2018-04-25
              • 2014-04-25
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              相关资源
              最近更新 更多