mercredi 24 juin 2015

How do I know what class called the abstract method?

say there are 2 classes...

public class NotAbstract1 extends AnAbstract {
  NotAbstract1() { super(); }
}

public class NotAbstract2 extends AnAbstract {
  NotAbstract2() { super(); }
}

public abstract class NnAbstract {
  AnAbstract() { //do something }
  abstract void saySomething() { System.out.println("Something"); }
}

In this example, NotAbstract1 and NotAbstract2 can call saySomething(). How can I, from within the saySomething() method of AnAbstract, recognize the class which called it? Without passing in the class or an identifier.

Again, the easy solution is to change the method signature to be saySomething(Class clazz) but I'd like to not do that. I have a feeling it can be done with reflection so I'm adding that tag to the question.





Aucun commentaire:

Enregistrer un commentaire