gpt4 book ai didi

java - 出现 "non-static method cannot be referenced from a static context"错误,不确定

转载 作者:行者123 更新时间:2023-12-02 08:08:00 25 4
gpt4 key购买 nike

Possible Duplicate:
What is the reason behind “non-static method cannot be referenced from a static context”?

public void Sort(){
*some code*
}
public void displayResults()
{*more code*
}

public static void main(String[] args)
{
Sort();
displayResults();
}

为什么我会收到此错误?我有排序();在另一个抽象类中,然后这个类正在扩展它。

-困惑

最佳答案

您需要实例化包含 Sort() 的类, displayResults()和 main 之前你可以打电话 Sort()displayResults()来自main() .

class Example {
public void Sort(){
// *some code*
}
public void displayResults()
{
// *more code*
}
public static void main(String[] args)
{
Example ex = new Example()
ex.Sort();
ex.displayResults();
}
}

关于java - 出现 "non-static method cannot be referenced from a static context"错误,不确定,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7862130/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com