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

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

    Not possible. Static methods are not runtime polymorphic in any way. It's absolutely impossible to distinguish these cases:

    System.out.println(Entity.getClass());
    System.out.println(User.getClass());
    

    They compile to the same byte code (assuming that the method is defined in Entity).

    Besides, how would you call this method in a way where it would make sense for it to be polymorphic?

提交回复
热议问题