本文介绍了是否在Java中继承了静态方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在阅读Khalid Mughal撰写的程序员指南
Java™SCJP认证

I was reading A Programmer’s Guide toJava™ SCJP Certification by Khalid Mughal.

在继承章节中,它解释

它还提到静态方法不会被继承。但是下面的代码完全没问题:

It also mentions that static methods are not inherited. But the code below is perfectlly fine:

class A
{
    public static void display()
    {
        System.out.println("Inside static method of superclass");
    }
}

class B extends A
{
    public void show()
    {
        // This works - accessing display() by its simple name -
        // meaning it is inherited according to the book.
        display();
    }
}

我如何直接使用在课堂 B 中显示()?更多, B.display()也有效。

How am I able to directly use display() in class B? Even more, B.display() also works.

本书的解释是否仅适用于实例方法?

Does the book's explanation only apply to instance methods?

推荐答案

所有可访问的方法都由子类继承。

All methods that are accessible are inherited by subclasses.

来自Sun Java :

From the Sun Java Tutorials:

继承静态的唯一区别(类)方法和继承的非静态(实例)方法是当你用相同的签名编写一个新的静态方法时,旧的静态方法只是隐藏,而不是被覆盖。

The only difference with inherited static (class) methods and inherited non-static (instance) methods is that when you write a new static method with the same signature, the old static method is just hidden, not overridden.

从了解覆盖和隐藏之间的区别。

From the page on the difference between overriding and hiding.

这篇关于是否在Java中继承了静态方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-22 21:26
查看更多