简体   繁体   English

关于静态和非静态方法

[英]About Static and Non-Static Methods

I know that Static means that variable or method belongs to class itself and we can use it with ClassName.StaticMethodName. 我知道静态意味着变量或方法属于类本身,我们可以将其与ClassName.StaticMethodName一起使用。 So the question is: We can use a non static method inside another nonstatic method but we cannot use a non static method in a static method. 所以问题是:我们可以在另一个非静态方法中使用非静态方法,但不能在静态方法中使用非静态方法。 I just do not understand why we can use a non static method inside another non static method? 我只是不明白为什么我们可以在另一个非静态方法中使用一个非静态方法? Do not we need an object to use non static method? 我们不需要使用非静态方法的对象吗? For Static methods we need to use Class name and it is enough. 对于静态方法,我们需要使用类名,这就足够了。 But, why we do not need an object to use a method? 但是,为什么我们不需要对象来使用方法呢? For example: 例如:

//This code will not generate any error. //此代码不会产生任何错误。 There is something else which I do not know? 还有其他我不知道的东西吗? Maybe C# add a hidden object before methodB()? 也许C#在methodB()之前添加了一个隐藏对象?

ClassA()
{
  public void methodA()
  {
    methodB();
  }
  public void methodB()
  {
  }
}

I just do not understand why we can use a non static method inside another non static method? 我只是不明白为什么我们可以在另一个非静态方法中使用一个非静态方法?

To call a non-static method, you need an object instance which you can call the method on. 要调用非静态方法,需要一个对象实例,可以在其上调用该方法。 In this example, MethodA cannot be called without an instantiated instance of ClassA. 在此示例中,如果没有ClassA的实例化实例,则无法调用MethodA。

For this reason, we know that if we are inside MethodA, there must be an existing instance this function is being executed on. 因此,我们知道如果我们在MethodA内,则必须存在一个正在执行此函数的现有实例。 For this reason, calling MethodB is valid as it is being called on the same object MethodA is currently running against. 出于这个原因,调用MethodB是有效的,因为它正在与当前正在运行的MethodA相同的对象上被调用。

Static basically means that you don't have to create a new instance of an object to use its methods. Static基本上意味着您不必创建对象的新实例即可使用其方法。

public static class ClassA
{
     public static void Run()
     {

     }
}

This can be called like ClassA.Run(); 可以像ClassA.Run();一样ClassA.Run(); .

public class ClassB
{
     public void Run()
     {

     }
}

To execute Run you need to do this: 要执行Run您需要执行以下操作:

ClassB b = new ClassB(); //Create a new Instance of type ClassB
b.Run();                

So when you got another method in ClassB which is invoked inside of Run() this.AnyOtherMethod(); 因此,当您在ClassB获得另一个在Run()内部调用的方法时, this.AnyOtherMethod(); (while this is redundant) will use the same instance Run() was invoked from. this是多余的),将使用从中调用Run()的相同实例。

Take for example a situation where you have a class with static and non-static method: 例如,您有一个带有静态和非静态方法的类:

public class A
{
    private int a;

    public A(int value)
    {
        this.a = value;
    }

    public static int MethodA()
    {
        return MethodB();
    }

    public int MethodB()
    {
        return a;
    }
}

Imagine that we are able to compile this, and we try to use this class without constructing it (creating an instance of a class). 想象一下我们能够编译它,并且我们尝试使用该类而不构造它(创建一个类的实例)。 We would not know the value of 'a' at run time. 我们不会在运行时知道'a'的值。 Therefore we cannot use instance methods inside static methods. 因此,我们不能在静态方法中使用实例方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM