我有以下两个Java类(Command和Player)。我希望能够从Command类中列出Player类中的所有公共方法,而不必对它们进行硬编码。我尝试了几种方法,包括接口或命令模式,但我不熟悉Java,我没有设法让它们工作。
在我看来,使用Java的反射API会更容易。但是,当我使用它从Player类打印公共方法时,我得到了无关的方法,我认为这些方法是从Object类继承的。
有没有办法只包含我自己定义的那些方法(即那些以“public method:public void Player。”开头的方法?)?
谢谢,
LC
以下是我得到的输出:
public method: public void Player.search(Command)
public method: public Room Player.getCurrentRoom()
public method: public void Player.engage()
public method: public void Player.trade(Command)
public method: public void Player.goRoom(Command)
public method: public void Player.takeItem(Command)
public method: public void Player.dropItem(Command)
public method: public void Player.lock(Command)
public method: public void Player.unlock(Command)
public method: public final void java.lang.Object.wait(long,int) throws java.lang.InterruptedException
public method: public final native void java.lang.Object.wait(long) throws java.lang.InterruptedException
public method: public final void java.lang.Object.wait() throws java.lang.InterruptedException
public method: public boolean java.lang.Object.equals(java.lang.Object)
public method: public java.lang.String java.lang.Object.toString()
public method: public native int java.lang.Object.hashCode()
public method: public final native java.lang.Class java.lang.Object.getClass()
public method: public final native void java.lang.Object.notify()
public method: public final native void java.lang.Object.notifyAll()
这是Command类:
import java.lang.reflect.Method;
public class Command
{
// a constant array that holds all valid command words
private static String[] validCommands;
private String commandWord;
private String secondWord;
public Command(String firstWord, String secondWord)
{
commandWord = firstWord;
this.secondWord = secondWord;
}
[...] //some code omitted
public boolean process(Player player)
{
Class pClass = player.getClass();
Method[] methods = pClass.getMethods();
for (int i = 0; i < methods.length; i++) {
System.out.println("public method: " + methods[i]);
}
boolean wantToQuit = false;
if(commandWord == null) {
System.out.println("I don't know what you mean...");
return false;
}
if (commandWord.equals("help")) {
printHelp();
}
else if (commandWord.equals("go")) {
player.goRoom(this);
}
else if (commandWord.equals("quit")) {
wantToQuit = quit();
}
else if (commandWord.equals("take")) {
player.takeItem(this);
}
else if (commandWord.equals("drop")) {
player.dropItem(this);
}
else if (commandWord.equals("search")) {
player.search(this);
}
else if (commandWord.equals("engage")) {
player.engage();
}
else if (commandWord.equals("trade")) {
player.trade(this);
}
else if (commandWord.equals("lock")) {
player.lock(this);
}
else if (commandWord.equals("unlock")) {
player.unlock(this);
}
else {
System.out.println("Invalid command. Type 'help' if you forgot the list of available commands.");
}
// else command not recognised.
return wantToQuit;
}
}
以下是Player类的概述:
public class Player
{
private String name;
private Room currentRoom;
private ArrayList<Item> items;
Player (String name, Room startingRoom)
{
this.name = name;
items = new ArrayList<Item>();
this.currentRoom = startingRoom;
printWelcome();
}
public void engage()
{
[...]
}
public void trade(Command command)
{
[...] }
public void goRoom(Command command)
{
[...] }
public void search(Command command)
{
[...] }
public void takeItem(Command command)
{
[...] }
public void dropItem(Command command)
{
[...] }
public void lock(Command command)
{
[...] }
public void unlock(Command command)
{
[...]
}
}
答案 0 :(得分:2)
您希望使用Java反射API中的getDeclaredMethods()类。
http://docs.oracle.com/javase/7/docs/api/java/lang/Class.html#getDeclaredMethods()
它只为您提供在相关类上声明的方法,忽略超类和接口。