我有以下两个 Java 类(Command 和 Player)。我希望能够从 Command 类中列出 Player 类中的所有公共(public)方法,而不必对它们进行硬编码。我尝试了多种方法,包括接口(interface)或命令模式,但我对 Java 不熟悉,无法使它们工作。
在我看来,使用Java的反射API会更容易。但是,当我使用它从 Player 类打印公共(public)方法时,我得到了无关的方法,我认为这些方法是从 Object 类继承的。
有没有办法只包含我自己定义的那些方法(即以“public method: public void Player.”开头的方法)?
谢谢,
液相色谱
这是我得到的输出:
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()
这是命令类:
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)
{
[...]
}
}
最佳答案
您想要使用 Java 反射 API 中的 getDeclaredMethods() 类。
http://docs.oracle.com/javase/7/docs/api/java/lang/Class.html#getDeclaredMethods()
这只会为您提供在相关类上声明的方法,忽略父类(super class)和接口(interface)。
关于java - 如何列出不被继承的方法?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22799106/