您如何在一个方法(functionA)中编写对不接收任何参数的另一类(Category1)中的方法(functionB)的调用?

嗨,这是班上的一个问题,我试图理解这个问题并解决它,但是很困难。我试图看书并搜索网络以弄清楚。

这就是我刚刚写的。。但是,我不知道它是对的还是接近的。

public void addFunctionB (Category1 Category1, String functionB) {
    Category1.setFunctionB(functionB);
}

最佳答案

我认为这就是您要寻找的。

public void functionA(){ // Function A which calls Function B
    Category1 category1 = new Category1(); // Creating an instance of Category1 which has the function B
    category1.functionB(); // Using the instance created above to call the method functionB
}


您的FunctionB也可以是static,因此,在这种情况下,您无需实例化Category1即可对其进行调用。您可以直接这样称呼它

Category1.functionB(); // Calling the method statically.

关于java - 您如何在一个方法(函数A)中编写对没有参数的另一类(类别1)中的方法(函数)的调用?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/19043716/

10-10 13:33