문제

I have next classes:

abstract public class Parent{
    static public void logRequestor(){
        //String requestor = // How to get requestor?
        //Log.e("Requestor is: " + requestor);
    }
}

class ChildA extends Parent{
}

class ChildB extends Parent{
}

Somewhere I have next lines:

ChildA.logRequestor();
ChildB.logRequestor();
ChildA.logRequestor();

How to know, which child are called static parent method with no params in function logRequestor? I want to have next logs:

Requestor is ChildA
Requestor is ChildB
Requestor is ChildA
도움이 되었습니까?

해결책 2

That is not possible, statics are defined in the class not in the instance therefore statics cannot be e.g. inherited und therefore cannot be used for polymorphism.

See this answer for more details: Why doesn't Java allow overriding of static methods?

다른 팁

Static methods are not inherited.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top