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

前端 未结 12 2279
滥情空心
滥情空心 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:55

    If i am taking it right you want to use your sub class in base class in static method I think you can do this by passing a class parameter to the method

    class Entity {
        public static void useClass(Class c) {
            System.out.println(c);
            // to do code here
        }
    }
    
    class User extends Entity {
    }
    
    class main{
        public static void main(String[] args){
            Entity.useClass(Entity.class);
        }
    }
    

提交回复
热议问题