Java 从数组调用方法

Java 从数组调用方法,java,arrays,methods,call,Java,Arrays,Methods,Call,Java中有没有从数组调用方法的方法?我想设计一个原始的棋盘游戏,我想使用一系列方法来表示游戏空间。也许你需要使用某种命令模式,比如 class Board { Cell[][] cells = new Cell[5][5]; void addCell(int i, int j, Cell cell) { cells[i,j] = cell; } void executeCell(int i, int j) { cells[i,j].execute

Java中有没有从数组调用方法的方法?我想设计一个原始的棋盘游戏,我想使用一系列方法来表示游戏空间。

也许你需要使用某种命令模式,比如

class Board {
   Cell[][] cells = new Cell[5][5];

   void addCell(int i, int j, Cell cell) {
     cells[i,j] = cell;
   }

   void executeCell(int i, int j) {
     cells[i,j].execute(this);
   }
}

interface Cell {
   void execute(Board board);
}

class CellImpl implements Cell {
  void execute(Board board) {
    // do your stuff here
  }
}

只要实现了Cell interface,您就可以添加任意多的实现—board可以执行它们。

也许您需要使用某种命令模式,如

class Board {
   Cell[][] cells = new Cell[5][5];

   void addCell(int i, int j, Cell cell) {
     cells[i,j] = cell;
   }

   void executeCell(int i, int j) {
     cells[i,j].execute(this);
   }
}

interface Cell {
   void execute(Board board);
}

class CellImpl implements Cell {
  void execute(Board board) {
    // do your stuff here
  }
}
只要实现了Cell接口,您就可以添加任意多的实现,board可以执行它们。

这是基本思想(命令模式)

输出

method-1
或者带着沉思

static Method[] methods = new Method[10];

public static void method1() {
    System.out.println("method-1");
}

public static void method2() {
    System.out.println("method-2");
}

public static void main(String[] args) throws Exception {
    methods[0] = Test1.class.getDeclaredMethod("method1");
    methods[1] = Test1.class.getDeclaredMethod("method2");
    methods[1].invoke(null);
}
这是基本思想(命令模式)

输出

method-1
或者带着沉思

static Method[] methods = new Method[10];

public static void method1() {
    System.out.println("method-1");
}

public static void method2() {
    System.out.println("method-2");
}

public static void main(String[] args) throws Exception {
    methods[0] = Test1.class.getDeclaredMethod("method1");
    methods[1] = Test1.class.getDeclaredMethod("method2");
    methods[1].invoke(null);
}

在数组中调用方法,将板单元传递给方法。在数组中调用方法,将板单元传递给方法。