非静态方法作用静态



试图从我的主要类中调用类非静态方法,制作了主类实例,并尝试从非静态方法运行该方法,但是我仍然继续获取"无法从静态上下文中引用非静态方法"错误。

主类看起来像这样;

public class WeatherController {
    public static void main(String[] args) {
        WeatherController mainController = new WeatherController();
        mainController.doStuff();
    }
    public void doStuff() {
        WeatherObservation newObservation = new WeatherObservation("Whyalla", "28-02-17", 38, 0, 1.3, 1);
        WeatherObservation.printObservation(newObservation);  
        WeatherHistory newHistory = new WeatherHistory();   //Create new History Array
        newHistory.arrayAdd(newObservation);                //Add the Observation to it.
// These are the problem methods:
        WeatherHistory.arrayPrint(newHistory);
        WeatherObservation.setTemp(10);
     }
} // End Class

dostuff应该是非静态的,因为我在Main Controller的实例上运行它,对吗?但是它无法调用settemp或arrayprint。

WeatherHistory.arrayPrint(newHistory);
WeatherObservation.setTemp(10);

这些是静态调用,用以下代码替换它们:

newHistory.arrayPrint(newHistory);
newObservation.setTemp(10);

相关内容

  • 没有找到相关文章

最新更新