簡體   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