Getting the name of a sub-class from within a super-class

前端 未结 12 2280
滥情空心
滥情空心 2020-12-14 05:31

Let\'s say I have a base class named Entity. In that class, I have a static method to retrieve the class name:

class Entity {
    public static         


        
12条回答
  •  生来不讨喜
    2020-12-14 05:50

    My context: superclass Entity with subclasses for XML objects. My solution: Create a class variable in the superclass

    Class claz;
    

    Then in the subclass I would set the variable of the superclass in the constructor

    public class SubClass {
       public SubClass() {
         claz = this.getClass();
       }
    }
    

提交回复
热议问题