繁体   English   中英

Java中的静态和非静态方法互调用

[英]Static And Non Static Method Intercall In Java

我正在清除我对 Java 的概念。 我对 Java 的了解还远远不够,所以请多多包涵。

我试图理解静态方法和非静态方法的相互调用。 我知道 -

  1. 静态方法可以在同一个类中简单地通过其名称调用另一个静态方法。
  2. 静态方法只有在创建类的实例后才能调用同一个类的另一个非静态方法。
  3. 非静态方法可以简单地通过 classname.methodname 调用同一类的另一个静态方法 - 不确定这是否正确?

我的问题是关于对同一类的另一个非静态方法的非静态方法调用。 在类声明中,当我们声明所有方法时,是否可以从非静态类中调用同一类的另一个非静态方法?
请举例说明。 谢谢你。

您的 #3 是正确的,您可以使用 classname.methodname 从非静态方法调用静态方法。

而且您的问题似乎是在问您是否可以从其他非静态方法调用类中的非静态方法,这也是可能的(也是最常见的)。

例如:

public class Foo {

public Foo() {
    firstMethod();
    Foo.staticMethod(); //this is valid
    staticMethod(); //this is also valid, you don't need to declare the class name because it's already in this class. If you were calling "staticMethod" from another class, you would have to prefix it with this class name, Foo
}

public void firstMethod() {
    System.out.println("This is a non-static method being called from the constructor.");
    secondMethod();
}

public void secondMethod() {
    System.out.println("This is another non-static method being called from a non-static method");

}

public static void staticMethod() {
    System.out.println("This is the static method, staticMethod");
}
}

方法是并且应该首先在语义上绑定到类或实例。

某事物的 List 具有长度或大小,因此您要求特殊 List 的大小。 您需要该类的对象来调用.size ()

一个典型的、众所周知的静态方法示例是Integer.parseInt ("123"); . 在那一刻你没有一个 Integer 实例,但想创建一个。

如果我们将那个方法绑定到一个实例,我们会将它绑定到一个 String 实例——这是有道理的:

int a = "123".parseInt ();

这本来是一个合理的选择,但这意味着类似的 float、double、long、short、Boolean 方法以及每个具有对称“toString”方法的类都必须放入 String 中。 那将意味着对 String 类进行无数扩展。

相反,String 是最终的,因此放置此类方法的合理位置是目标类,例如 Integer、Float 等。

不确定我是否正确理解了这个问题,但非静态方法是在 OO 中设计类的标准方法。 也许这个示例将有助于引发讨论:

public class MySampleClass{

   private void methodA(){
       System.out.println('a called');
   }

   public void methodB(){
       this.methodA();
       staticMethod(this);
   }

   private static void staticMethod( MySampleClass inst ){
       inst.methodA(); 
   }
}
public class TestClass{

  public static void testStatic(){
   System.out.println("test1");
  }

  public void testNonStatic(){
    System.out.println("test2");
  }

  public void test1(){
    // both is valid
    testStatic();  
    TestClass.testStatic();

    // this is valid, cause it can call the method of the same instance of that class
    testNonStatic();   
    this.testNonStatic();

    // this is not valid, cause without a concrete instance of a class you cannot call
    // non static methods
    TestClass.testNonStatic();
  }

  public static void test2(){
    // again these are both correct
    testStatic();  
    TestClass.testStatic();

    // this doesn't work, cause you cannot call non static methods out of static methods
    testNonStatic();   
    this.testNonStatic();

    // this instead does work cause you have a concrete instance of the object
    TestClass myTestClass = new TestClass();
    myTestClass.testNonStatic();

    // this still doesn't work
    TestClass.testNonStatic();
  }

}

您可以使用对要调用该方法someObject.method对象的显式引用从非静态方法中调用非静态方法,或者不指定该对象someMethod() (在这种情况下,它将在与您相同的对象上调用)正在调用当前的非静态方法)。

也许这会更好地展示它

class Demo {
    private String name;

    public Demo(String n) {
        name = n;
    }

    public String getName() {// non static method
        return name;
    }

    public void test(Demo d) {// non-static method
        System.out.println("this object name is: "+getName());// invoking method on this object
        System.out.println("some other object name is: "+d.getName());// invoking method on some other object
    }
    //test
    public static void main(String[] args) {
        Demo d=new Demo("A");
        Demo d2=new Demo("B");
        d.test(d2);
    }
}

输出:

this object name is: A
some other object name is: B

暂无
暂无

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

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