2017-05-18 8 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

抽象クラスをインスタンス化することはできないので、この抽象クラスを 'extends 'するクラスを持たない限り、非静的(インスタンス)メソッドを実装する意味がありません。このメソッドを 'abstract'宣言し、抽象クラスを継承するクラスで実装を提供するか、このメソッドを' static'として宣言します –

関連する問題