繁体   English   中英

如何在Java中的另一个类的静态方法中调用非静态方法?

[英]How to call non-static method in another class's static method in 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
} 

任何帮助?

将包含非静态方法的类的实例传递到静态方法中

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(...);

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM