【问题标题】:How to know, which child are called static parent method?如何知道,哪些子方法称为静态父方法?
【发布时间】:2014-03-10 10:07:36
【问题描述】:

我有下一堂课:

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{
}

在某处我有下一行:

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

如何知道函数logRequestor 中没有参数的静态父方法称为哪个子方法?我想要下一个日志:

Requestor is ChildA
Requestor is ChildB
Requestor is ChildA

【问题讨论】:

  • 静态方法永远不会被继承。这就是 oracle 文档所说的至少

标签: java inheritance static-methods


【解决方案1】:

静态方法不会被继承。

【讨论】:

    【解决方案2】:

    这是不可能的,静态是在类中而不是在实例中定义的,因此静态不能是例如因此,继承的 und 不能用于多态性。

    更多详情请查看此答案:Why doesn't Java allow overriding of static methods?

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-01-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多