【问题标题】:Call a method of another class that has another class extended调用另一个类的方法,该类扩展了另一个类
【发布时间】:2014-05-19 17:35:44
【问题描述】:

如何调用另一个类扩展了另一个类的方法?

我有两个班级:

public class Main extends JavaPlugin {
    public void whatever() {
        // whatever
    }
}

public void AnotherClass implements Listener {
    // whatever
}

当我尝试在 AnotherClass 中调用 Main 的方法时:

public class AnotherClass implements Listener {
    Main main = new Main();

    main.whatever();
}

它不工作,但是当我调用一个没有扩展某些东西的函数的方法时它工作正常。

【问题讨论】:

  • 你得到什么错误信息?
  • 它不工作!你是什​​么意思?有什么错误吗?
  • public void AnotherClass implements Listener {
  • "不扩展某些东西的函数的方法"
  • 好吧奇怪。我通过不在第三个类中扩展 JavaPlugin 解决了这个问题。看起来一切正常。

标签: java class methods call extends


【解决方案1】:

我发现了一些问题。

public void AnotherClass implements Listener

你不能有 void 类。

一个文件中不能有两个 public 类。

你的类声明应该是这样的

class AnotherClass implements Listener

您还从任何方法外部调用main.whatever(),据我所知,这将不起作用,除非它位于static 块中。您需要从其他方法中调用方法。

class AnotherClass implements Listener {

    public void something() {
        Main main = new Main();
        main.whatever();
    }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-10-05
    • 2021-11-19
    • 1970-01-01
    • 2012-06-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多