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

标签 java methods static non-static

我的一个类(class)有这个方法

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

关于java - java中如何在另一个类的静态方法中调用非静态方法?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44056374/

相关文章:

java - 在 Spring MVC 配置中将 default-servlet-handler 放在哪里

java - 如何打印用户定义列表中最长的单词?

python - STATICFILES_DIRS 设置不是元组或列表。尽管它不包含逗号

c++ - 将继承的方法传递给另一个方法

python - 调用父类函数比较父子类

java - 在重复任务中调用 'this' 时出现静态问题

c++ - 为什么 `using`关键字不能用于静态类成员?

java - 有时速度并不能替代

java BufferedReader 特定长度返回NUL字符

java - 如何使用 Java 创 build 计二维码?