小编squ*_*ert的帖子

将静态方法作为参数发送,并在Java中的另一个方法中进行通用调用

我想使用一个类(一种类型)中的静态方法,

  1. 将它们发送到另一个类中的另一个方法,并且
  2. 在不明确使用其名称的情况下,在不同类的方法中调用它们

以使调用通用.

有没有办法实现这个功能?一些伪代码有助于举例说明我正在尝试做的事情:

public class A
{
     public static void aMethod1()
     {
          //do something
     }

     public static void aMethod2()
     {
          //do something else
     }
}

public class B
{
     public void bMethod(<Parameter that can take any of A's methods polymorphically>)
     {
          <call ANY method sent in by the parameter with the same line of code>
     }
}

public class Main
{
     public static void main(String[] args)
     {
          A obj_A = new A();
          B obj_B = new …
Run Code Online (Sandbox Code Playgroud)

java oop generics methods polymorphism

5
推荐指数
1
解决办法
754
查看次数

标签 统计

generics ×1

java ×1

methods ×1

oop ×1

polymorphism ×1