Java 如何列出未继承的方法?
我有以下两个Java类(Command和Player)。我希望能够从命令类中列出Player类中的所有公共方法,而不必硬编码它们。我尝试了几种方法,包括接口或命令模式,但我对Java不太精通,也没能让它们正常工作 在我看来,使用Java的反射API会更容易。但是,当我使用它从Player类打印公共方法时,我得到了一些无关的方法,我假设这些方法是从Object类继承的 有没有办法只包括我自己定义的那些方法(即那些以“public method:public void Player.”开头的方法) 谢谢, 信用证 以下是我得到的输出:Java 如何列出未继承的方法?,java,reflection,Java,Reflection,我有以下两个Java类(Command和Player)。我希望能够从命令类中列出Player类中的所有公共方法,而不必硬编码它们。我尝试了几种方法,包括接口或命令模式,但我对Java不太精通,也没能让它们正常工作 在我看来,使用Java的反射API会更容易。但是,当我使用它从Player类打印公共方法时,我得到了一些无关的方法,我假设这些方法是从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;
}
}
import java.lang.reflect.Method;
公共类命令
{
//保存所有有效命令字的常量数组
私有静态字符串[]有效命令;
私有字符串命令字;
私有字符串第二个字;
公共命令(字符串第一个字,字符串第二个字)
{
命令字=第一个字;
this.secondWord=secondWord;
}
[…]//省略了一些代码
公共布尔进程(播放器)
{
类pClass=player.getClass();
Method[]methods=pClass.getMethods();
for(int i=0;i
以下是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)
{
[...]
}
}
公共类播放器
{
私有字符串名称;
包房;
私有ArrayList项;
播放器(字符串名称,房间开始房间)
{
this.name=名称;
items=newarraylist();
this.currentRoom=启动室;
printWelcome();
}
公众参与
{
[...]
}
公共无效交易(命令)
{
[...] }
公共void goRoom(命令)
{
[...] }
公共无效搜索(命令)
{
[...] }
公共无效项目(命令)
{
[...] }
公共void dropItem(命令)
{
[...] }
公共无效锁(命令)
{
[...] }
公共无效解锁(命令)
{
[...]
}
}
您希望使用Java反射API中的getDeclaredMethods()类
这只提供了在相关类上声明的方法,忽略了超类和接口。您没有发布有趣的代码:在哪里使用反射。请参阅:。它位于命令类的process方法的开头:class pClass=player.getClass();Method[]methods=pClass.getMethods();对于(inti=0;i