2014-10-09 21 views
-2
class A{ 
    String z(){ 
     System.out.println("a"); 
     return "sauarbh"; 
    } 
} 
class B{ 
    A a; 
    A x(){ 
    return a; 
    } 
} 
public class runner { 
    public static void main(String[] args) { 
     B b = new B(); 
     A a2=b.x(); 
     a2.z(); // Calling A class method without creating object of it 
    } 
} 

另一示例調用類的方法,而無需創建它

class Person 
    { 
    private String lastName; 
    private String firstName; 
    private int age; 
//-------------------------------------------------------------- 
    public Person(String last, String first, int a) 
     {        // constructor 
     lastName = last; 
     firstName = first; 
     age = a; 
     } 
//-------------------------------------------------------------- 
    public void displayPerson() 
     { 
     System.out.print(" Last name: " + lastName); 
     System.out.print(", First name: " + firstName); 
     System.out.println(", Age: " + age); 
     } 
//-------------------------------------------------------------- 
    public String getLast()   // get last name 
     { return lastName; } 
    } // end class Person 
//////////////////////////////////////////////////////////////// 
class ClassDataArray 
    { 
    private Person[] a;    // reference to array 
    private int nElems;    // number of data items 

    public ClassDataArray(int max) // constructor 
     { 
     a = new Person[max];    // create the array 
     nElems = 0;      // no items yet 
     } 
//-------------------------------------------------------------- 
    public Person find(String searchName) 
     {        // find specified value 
     int j; 
     for(j=0; j<nElems; j++)   // for each element, 
     if(a[j].getLast().equals(searchName)) // found item? 
      break;      // exit loop before end 
     if(j == nElems)     // gone to end? 
     return null;     // yes, can't find it 
     else 
     return a[j];     // no, found it 
     } // end find() 
//--------------------------------------------------------------         // put person into array 
    public void insert(String last, String first, int age) 
     { 
     a[nElems] = new Person(last, first, age); 
     nElems++;       // increment size 
     } 
//-------------------------------------------------------------- 
    public boolean delete(String searchName) 
     {        // delete person from array 
     int j; 
     for(j=0; j<nElems; j++)   // look for it 
     if(a[j].getLast().equals(searchName)) 
      break; 
     if(j==nElems)      // can't find it 
     return false; 
     else        // found it 
     { 
     for(int k=j; k<nElems; k++)  // shift down 
      a[k] = a[k+1]; 
     nElems--;      // decrement size 
     return true; 
     } 
     } // end delete() 
//-------------------------------------------------------------- 
    public void displayA()   // displays array contents 
     { 
     for(int j=0; j<nElems; j++)  // for each element, 
     a[j].displayPerson();   // display it 
     } 
//-------------------------------------------------------------- 
    } // end class ClassDataArray 
//////////////////////////////////////////////////////////////// 
class ClassDataApp 
    { 
    public static void main(String[] args) 
     { 
     int maxSize = 100;    // array size 
     ClassDataArray arr;   // reference to array 
     arr = new ClassDataArray(maxSize); // create the array 
            // insert 10 items 
     arr.insert("Evans", "Patty", 24); 
     arr.insert("Smith", "Lorraine", 37); 
     arr.insert("Yee", "Tom", 43); 
     arr.insert("Adams", "Henry", 63); 
     arr.insert("Hashimoto", "Sato", 21); 
     arr.insert("Stimson", "Henry", 29); 
     arr.insert("Velasquez", "Jose", 72); 
     arr.insert("Lamarque", "Henry", 54); 
     arr.insert("Vang", "Minh", 22); 
     arr.insert("Creswell", "Lucinda", 18); 

     arr.displayA();    // display items 

     String searchKey = "Stimson"; // search for item 
     Person found; 
     found=arr.find(searchKey); 
     if(found != null) 
     { 
     System.out.print("Found "); 
     found.displayPerson(); 
     } 
     else 
     System.out.println("Can't find " + searchKey); 

     System.out.println("Deleting Smith, Yee, and Creswell"); 
     arr.delete("Smith");   // delete 3 items 
     arr.delete("Yee"); 
     arr.delete("Creswell"); 

     arr.displayA();    // display items again 
     } // end main() 
    } // end class ClassDataApp 

對象如在上面的代碼我打電話類A的Z()方法參照A2,而無需創建類的對象A,因爲我是新來的Java我想知道在顯示的代碼中的Java中這是什麼概念?現在我只知道如果我們想調用一個沒有創建它的對象的方法,我們必須使該方法爲靜態。

在使用人蔘考第二示例

發現,我們能夠調用displayPerson()方法沒有空指針異常

+1

'static'是你要找的關鍵詞。 – 2014-10-09 05:29:42

+2

如果您在類「B」中沒有實例「A」,那麼您將得到一個空指針異常。 – 2014-10-09 05:29:59

+0

上面的代碼應該拋出一個NullPointerException **,因爲**你從來沒有在B中創建一個新的A. – 2014-10-09 05:30:31

回答

4

要調用:

String z(){ 
     System.out.println("a"); 
     return "sauarbh"; 
    } 

而不類Az具有方法的對象是靜態的:

static String z(){ 
     System.out.println("a"); 
     return "sauarbh"; 
    } 

因此改變你的代碼如下:

class A{ 
    static String z(){ 
     System.out.println("a"); 
     return "sauarbh"; 
    } 
} 
class B{ 
    A a; 
    A x(){ 
    return a; 
    } 
} 
public class runner { 
    public static void main(String[] args) { 
     B b = new B(); 
     b.x(); 
     A.z(); 
    } 
} 

輸出:

a 
+1

我認爲'A.z()'是足夠的,如果'z'是靜態的。 – 2014-10-09 05:34:46

+1

要添加到@TAsk評論,您應該避免在引用上調用'static'方法,即使它們是'null'引用並且代碼在運行。 – 2014-10-09 05:35:34

+1

對變量調用'static'方法被認爲是一種不好的做法。更好的是做'A.z()' – 2014-10-09 05:35:48

0

是沒有實例化類,如果你想給你打電話應該使用static關鍵字的方法。

你在這裏做什麼?

您是間接嘗試獲取A的實例。但是,這種情況下,你會得到NullPointerException因爲你剛剛返回A

B b = new B(); 
A a2=b.x(); 
a2.z(); // NullPointerException from here 

NPE的只是一個引用(變量)?

class B{ 
    A a; 
    A x(){ 
    return a; // you just return the reference 
    // it should be return new A(); 
    } 
} 

爲了您的編輯:

看看insert()方法。它創建了Person實例。

0

B類方法x()不返回A的新對象。相反,您正在返回具有空值的A類對象。

A a; // value of a is null 
A x() { 
    return a; 
} 

在亞軍類

A a2=b.x(); // b.x() will return a, which is null. so A a2=null 
a2.z(); // Equivalent to null.z() throws NullPointerException 

進行更改下面的代碼Class B

class B{ 
    A a; 
    A x(){ 
    return new A();// return new object of Class A; 
    } 
} 

class B{ 
    A a= new A(); // Initialize Class A with new object 
    A x(){ 
    return a; 
    } 
} 
相關問題