2012-07-09 107 views
1

我正在清除我的Java概念。我對Java的瞭解遠在開端,對我很友善。Java中的靜態和非靜態方法Intercall

我想了解靜態方法和非靜態方法intercalls。我知道 -

  1. 靜態方法可以簡單地調用另一個靜態方法在同一類中的名稱。
  2. 只有在創建類的實例後,靜態方法才能調用同一類的另一個非staic方法。
  3. 非靜態方法可以簡單地通過classname.methodname調用同一類的另一個靜態方法 - 不知道這是否正確?

我的問題是關於非靜態方法調用另一個非staic相同類的方法。在類聲明中,當我們聲明所有方法時,我們可以從非靜態類中調用另一個非靜態方法嗎?
請用實例說明。謝謝。

+1

你寫了一個測試案例和嘗試,也許? – 2012-07-09 21:07:54

+0

我認爲如果你提供這個例子會更有幫助。用一兩個方法提供一個簡單的類聲明。包括您詢問的方法調用。然後我們可以說「是的,這是允許的」或者「不是這樣,這是原因」。 – 2012-07-09 21:09:25

+0

當然你可以做到這一點,只需編寫一個簡單的類:2個靜態方法,2個非靜態方法,並嘗試實現你所要求的,答案將足夠清晰。 – Benjamin 2012-07-09 21:15:15

回答

0

不確定我是否正確理解問題,但非靜態方法是在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(); 
    } 
} 
6

你的#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"); 
} 
} 
0
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(); 
    } 

} 
2

一種方法是,應該在第一方面在語義上結合至任一類或實例。

一個東西的列表有一個長度或大小,所以你問的特殊列表的大小。你需要該類的一個對象來呼叫.size()

一個典型的,衆所周知的靜態方法的例子是Integer.parseInt ("123");。您在那一刻沒有Integer實例,但想要創建一個實例。

如果在所有的,我們將這個方法綁定到一個實例,我們將其綁定到一個String實例 - 這將使意義:

int a = "123".parseInt(); 

這本來是一個合理的選擇,但它會意味着對於float,double,long,short,Boolean和可能每個具有對稱「toString」方法的類都必須放入String中。這將意味着對String類的數以萬計的擴展。

相反,String是最終的,所以放置這樣一個方法的合理位置是目標類,如Integer,Float等。

+1

這是我清除所有疑惑的最好解釋!謝謝朋友 – 2013-05-17 03:56:36

0

您可以使用顯式引用您要調用該方法的對象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