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

2019-01-22 16:37发布

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 String getClass() {
        return Entity.class.getClass();
    }
}

Now I have another class extend that.

class User extends Entity {
}

I want to get the class name of User:

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

My goal is to see "com.packagename.User" output to the console, but instead I'm going to end up with "com.packagename.Entity" since the Entity class is being referenced directly from the static method.

If this wasn't a static method, this could easily be solved by using the this keyword within the Entity class (i.e.: return this.class.getClass()). However, I need this method to remain static. Any suggestions on how to approach this?

12条回答
我欲成王,谁敢阻挡
2楼-- · 2019-01-22 17:25

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.

查看更多
▲ chillily
3楼-- · 2019-01-22 17:25
  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.

查看更多
该账号已被封号
4楼-- · 2019-01-22 17:26

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();
    }
}
查看更多
贪生不怕死
5楼-- · 2019-01-22 17:28

it is very simple done by

User.getClass().getSuperclass()

查看更多
Luminary・发光体
6楼-- · 2019-01-22 17:29

A static method is associated with a class, not with a specific object.

Consider how this would work if there were multiple subclasses -- e.g., Administrator is also an Entity. How would your static Entity method, associated only with the Entity class, know which subclass you wanted?

You could:

  • Use the existing getClass() method.
  • Pass an argument to your static getClass() method, and call an instance method on that object.
  • Make your method non-static, and rename it.
查看更多
\"骚年 ilove
7楼-- · 2019-01-22 17:32

This works for me

this.getClass().asSubclass(this.getClass())

But I'm not sure how it works though.

查看更多
登录 后发表回答