Issue
I want to get all methods in class that extends class2(abstract class), but it also gets method in class2
for example,
public static boolean hasClickedPlayerMethod(Ability ability) {
for (Method method : ability.getClass().getMethods()) {
if (!method.getName().equals("activeSkill")) continue;
System.out.println(method); // returns activeSkill(Player clickedPlayer), activeSkill()
}
return false;
}
// abstract class Ability
public boolean activeSkill() {
return false;
}
public boolean activeSkill(Player clickedPlayer) {
return false;
}
// Class BoatMan extends Ability
public BoatMan(Player player) {
super(...)
}
public boolean activeSkill() {
}
...
In this, I want to get only
public boolean activeSkill() {
but it also gets public boolean activeSkill(Player player) {
how can I get only public boolean activeSkill() {
? (BoatMan class has only public boolean activeSkill() {
but extend class has public boolean activeSkill(Player) {
Solution
Use
ability.getClass().getDeclaredMethods()
getDeclaredMethods: "Returns an array containing Method objects reflecting all the declared methods of the class or interface represented by this Class object, including public, protected, default (package) access, and private methods, but excluding inherited methods."
Answered By - sridhar
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.