简体   繁体   English

从静态方法调用非静态方法

[英]Invoke a non static method from a static method

谁能解释一下为什么静态方法调用非静态方法是非法的?

A non-static method is an instance method which acts upon a specific instance (object), and has access to that object's members. 非静态方法是一种作用于特定实例 (对象)并可以访问该对象成员的实例方法。 A static method is not associated with a specific instance. 静态方法不与特定实例相关联。

It is legal to call a non-static method if you provide an instance: 如果提供实例,则调用非静态方法是合法的:

class Foo
{
    public static void f()
    {
        instanceMethod(); // Not OK - no instance provided.
        this.instanceMethod(); // Not OK -  no "this" in static methods.
        new Foo().instanceMethod();  // OK because you provide an instance
    }

    public void instanceMethod() {}
}

because to call static method you don't need object of class, where to call non static method you need, 因为调用静态方法不需要类的对象,在哪里需要非静态方法,

Static method is associated with class where non static method is associated with state (Object) 静态方法与类相关联,而非静态方法与状态(对象)相关联

A non-static method is a method that executes in the context of an instance . 非静态方法是在实例的上下文中执行的方法。 Without an instance it makes no sense to call one, so the compiler prevents you from doing so - ie it's illegal. 没有实例,调用一个实例是没有意义的,因此编译器会阻止您这样做-即是非法的。

If a method doesn't access any instance fields (aka instance variables) then it should probably be changed to be a static method. 如果某个方法不访问任何实例字段(又称实例变量),则应将其更改为静态方法。

非静态方法具有隐式参数this ,而静态方法则没有(因为没有this ),因此,不可能将非静态方法称为静态方法。

Static methods belong to the class. 静态方法属于该类。 Non static methods belong to an instance. 非静态方法属于一个实例。

The non static method require an instance ( this ) to be executed on. 非静态方法需要执行实例( this )。 The static method don't require any instance, since the class itself is the instance to which it belongs. 静态方法不需要任何实例,因为类本身就是它所属的实例。

If I have 100 object instance, every nonStatic() method invocation affects the state of a different object. 如果我有100个对象实例,则每个nonStatic()方法调用都会影响另一个对象的状态。 So every nonStatic method is 'different' because it acts on different entities. 因此,每种非静态方法都是“不同的”,因为它作用于不同的实体。 At the same time, even if I have 100 instance of an object, calling staticMethod() doesn't affect any of those instances, because it can only affect the static members of the class. 同时,即使我有一个对象的100个实例,调用staticMethod()也不会影响这些实例中的任何一个,因为它只会影响该类的静态成员。

There is 1 class and 1 instance of every static methods or static member variables. 每个静态方法或静态成员变量都有1个类和1个实例。

For the non static members we have every single method associated to the class instance. 对于非静态成员,我们具有与类实例关联的每个方法。

So if you would be able to call the nonStatic method, from a static method, which of the 100 instance would be affected? 因此,如果您能够从静态方法中调用nonStatic方法,那么将影响100个实例中的哪一个? You see: it doesn't make sense. 您会看到:这没有道理。

Static methods are connected with the class. 静态方法与该类连接。 Regular methods are connected to the object. 常规方法连接到对象。

In static method there is no object on which you can run the non-static method. 在静态方法中,没有可以运行非静态方法的对象。

It is not illegal to call a non-static method from a static method. 从静态方法中调用非静态方法是非法的。 Whenever you invoke a static method there is no need of an instance of a class whereas in case of non-static case you do. 每当调用静态方法时,都不需要类的实例,而在非静态情况下则需要。

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

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