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

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

    it is very simple done by

    User.getClass().getSuperclass()

    0 讨论(0)
  • 2020-12-14 05:57

    Your question is ambiguous but as far as I can tell you want to know the current class from a static method. The fact that classes inherit from each other is irrelevant but for the sake of the discussion I implemented it this way as well.

    class Parent {
        public static void printClass() {
          System.out.println(Thread.currentThread().getStackTrace()[2].getClassName());
        }
    }
    
    public class Test extends Parent {
        public static void main(String[] args) {
          printClass();
        }
    }
    
    0 讨论(0)
  • 2020-12-14 06:00

    Don't make the method static. The issue is that when you invoke getClass() you are calling the method in the super class - static methods are not inherited. In addition, you are basically name-shadowing Object.getClass(), which is confusing.

    If you need to log the classname within the superclass, use

    return this.getClass().getName();
    

    This will return "Entity" when you have an Entity instance, "User" when you have a User instance, etc.

    0 讨论(0)
  • 2020-12-14 06:03
    1. Create a member String variable in the superclass.
    2. Add the this.getClass().getName() to a constructor that stores the value in the member String variable.
    3. Create a getter to return the name.

    Each time the extended class is instantiated, its name will be stored in the String and accessible with the getter.

    0 讨论(0)
  • 2020-12-14 06:06

    Why do you want to implement your own getClass() method? You can just use

    System.out.println(User.class);
    

    Edit (to elaborate a bit): You want the method to be static. In that case you must call the method on the class whose class name you want, be it the sub-class or the super-class. Then instead of calling MyClass.getClass(), you can just call MyClass.class or MyClass.class.getName().

    Also, you are creating a static method with the same signature as the Object.getClass() instance method, which won't compile.

    0 讨论(0)
  • 2020-12-14 06:08

    The superclass should not even know of the existence of the subclass, much less perform operations based on the fully qualified name of the subclass. If you do need operations based on what the exact class is, and can't perform the necessary function by inheritance, you should do something along these lines:

    public class MyClassUtil
    {
        public static String doWorkBasedOnClass(Class<?> clazz)
        {
            if(clazz == MyNormalClass.class)
            {
                // Stuff with MyNormalClass
                // Will not work for subclasses of MyNormalClass
            }
    
            if(isSubclassOf(clazz, MyNormalSuperclass.class))
            {
                // Stuff with MyNormalSuperclass or any subclasses
            }
    
            // Similar code for interface implementations
        }
    
        private static boolean isSubclassOf(Class<?> subclass, Class<?> superclass)
        {
            if(subclass == superclass || superclass == Object.class) return true;
    
            while(subclass != superclass && subclass != Object.class)
            {
                subclass = subclass.getSuperclass();
            }
            return false;
        }
    }
    

    (Untested code)

    This class doesn't know about its own subclasses, either, but rather uses the Class class to perform operations. Most likely, it'll still be tightly linked with implementations (generally a bad thing, or if not bad it's not especially good), but I think a structure like this is better than a superclass figuring out what all of its subclasses are.

    0 讨论(0)
提交回复
热议问题