Java: get the class of the inherited class from the static method

I have the following problems in Java:

class Base
{
    static void foo()
    {
        // I want to get Derived class here
        // Derived.class
    }
}

class Derived extends Base
{
}

Derived.foo();

Thanks for your help!

David

Solution

This is not how static methods work You will have to implement derived Foo (), execute the special function of Derived, and then call Base.. Foo () method If you really need type information, you can create base Foo0 (class Klass)

But to be honest, any static method of a class that needs to know the calling class should be an instance method

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>