2017-05-18 92 views
0

我已經在我的課如何在java中另一個類的靜態方法中調用非靜態方法?

@Override 
public void liveFlights(int one, int two, int three, int four, int five, 
         int six, int seven, int eight, int nine, int ten, int eleven, int twelve) { 
    System.out.println("There is "+counter+" flights at this moment "); 

    System.out.println("England to Netherland at 12.00 pm "+one+"is flight number"); 
    System.out.println("Netherland to England at 12.00 pm "+two+"is flight"); 
    System.out.println("Turkey to USA at 06.00 pm "+three+"is flight number"); 

的一個這種方法,我想稱之爲非靜態方法在這個靜態方法(其他類)

public static void searchResEntry() throws java.io.IOException // start of method 
{ 
    // variable declarations 
    String Name; 
    String Address; 
    Scanner read = new Scanner(System.in); 
    System.out.print("Please enter your following information for your reservation result: \n"); // displaying the information to enter for searching a reservation 

    System.out.print(" Enter Name : "); // displaying message to enter name 
    Name = read.nextLine(); // prompt for the name 
} 

任何幫助?

+0

你的靜態方法則需要包含非類的一個實例靜態方法。 – NAMS

回答

1
包含非靜態方法分爲靜態方法,它

public static void searchResEntry(YourClass instance) throws java.io.IOException // start of method 
    { 
     // variable declarations 
     instance.liveFlights(...); 

或創建實例類的

通行證實例:

public static void searchResEntry() throws java.io.IOException // start of method 
    { 
     // variable declarations 
     YourClass instance = new YourClass(); 
     instance.liveFlights(...); 
+0

我忘了添加,非靜態方法是在我的抽象類,爲什麼實例認爲不工作。它取決於? –

+0

既然你不能實例化抽象類,除非你有一些類來擴展這個抽象類,否則在它中實現非靜態(實例)方法是沒有意義的。要麼聲明這個方法是'abstract',並且在擴展抽象類的類中提供實現,或者將此方法聲明爲'static' –

相關問題