Variable and Method Hiding in Java – Java中的变量和方法隐藏

最后修改: 2018年 5月 17日

中文/混合/英文(键盘快捷键:t)

1. Introduction

1.介绍

In this tutorial, we’re going to learn about variable and method hiding in the Java language.

在本教程中,我们将学习Java语言中的变量和方法隐藏

First, we’ll understand the concept and purpose of each of these scenarios. After that, we’ll dive into the use cases and examine different examples.

首先,我们将了解每个场景的概念和目的。之后,我们将深入到用例中,研究不同的例子。

2. Variable Hiding

2.变量的隐藏

Variable hiding happens when we declare a property in a local scope that has the same name as the one we already have in the outer scope.

当我们在局部作用域中声明一个与我们在外部作用域中已有的属性名称相同的属性时,就会发生变量隐藏。

Before jumping to the examples, let’s briefly recap the possible variable scopes in Java. We can define them with the following categories:

在跳到例子之前,让我们简单回顾一下Java中可能的变量作用域。我们可以用以下类别来定义它们。

  • local variables – declared in a piece of code such as methods, constructors, in any block of code with curly braces
  • instance variables – defined inside of a class and belong to the instance of the object
  • class or static variables – are declared in the class with the static keyword. They have a class level scope.

Now, let’s describe the hiding with examples, for each individual category of variables.

现在,让我们用例子来描述隐藏的情况,对于每一个单独的变量类别。

2.1. The Power of Local

2.1.地方的力量

Let’s have a look at the HideVariable class:

让我们看一下HideVariable类。

public class HideVariable {

    private String message = "this is instance variable";

    HideVariable() {
        String message = "constructor local variable";
        System.out.println(message);
    }

    public void printLocalVariable() {
        String message = "method local variable";
        System.out.println(message);
    }

    public void printInstanceVariable() {
        String message = "method local variable";
        System.out.println(this.message);
    }
}

Here we have the message variable declared in 4 different places. The local variables declared inside of the constructor and the two methods are shadowing the instance variable.

这里我们有一个message变量在4个不同的地方声明。在构造函数和两个方法中声明的局部变量是对实例变量的阴影。

Let’s test the initialization of an object and calling the methods:

让我们测试一下对象的初始化和方法的调用。

HideVariable variable = new HideVariable();
variable.printLocalVariable();

variable.printInstanceVariable();

The output of the code above is:

上述代码的输出是。

constructor local variable
method local variable
this is instance variable

Here, the first 2 calls are retrieving the local variables.

这里,前两个调用是在检索局部变量。

To access the instance variable from the local scope, we can use this keyword like it is shown in printInstanceVariable() method.

为了从本地范围访问实例变量,我们可以使用this关键字,如printInstanceVariable()方法中所示。

2.2. Hiding and the Hierarchy

2.2.隐藏和等级制度

Similarly, when both the child and the parent classes have a variable with the same name, the child’s variable hides the one from the parent.

同样地,当子类和父类都有一个同名的变量时,子类的变量会隐藏父类的变量。

Let’s suppose we have the parent class:

让我们假设我们有父类。

public class ParentVariable {

    String instanceVariable = "parent variable";

    public void printInstanceVariable() {
        System.out.println(instanceVariable);
    }
}

After that we define a child class:

之后,我们定义一个子类。

public class ChildVariable extends ParentVariable {

    String instanceVariable = "child variable";

    public void printInstanceVariable() {
        System.out.println(instanceVariable);
    }
}

To test it, let’s initialize two instances. One with parent class and another with the child, then invoke the printInstanceVariable() methods on each of them:

为了测试它,让我们初始化两个实例。一个是父类,另一个是子类,然后对它们分别调用printInstanceVariable()方法。

ParentVariable parentVariable = new ParentVariable();
ParentVariable childVariable = new ChildVariable();

parentVariable.printInstanceVariable();
childVariable.printInstanceVariable();

The output shows the property hiding:

输出显示隐藏的属性。

parent variable
child variable

In most cases, we should avoid creating variables with the same name both in parent and child classes. Instead, we should use a proper access modifier like private and provide getter/setter methods for that purpose.

在大多数情况下,我们应该避免在父类和子类中创建同名的变量。相反,我们应该使用适当的访问修饰符,如private,并为此提供getter/setter方法。

3. Method Hiding

3.方法隐藏

Method hiding may happen in any hierarchy structure in java. When a child class defines a static method with the same signature as a static method in the parent class, then the child’s method hides the one in the parent class. To learn more about the static keyword,  this write-up is a good place to start.

方法隐藏可能发生在java的任何层次结构中。当一个子类定义的静态方法与父类中的静态方法具有相同的签名时,那么子类的方法就会隐藏父类中的方法。要了解更多关于static关键字的信息,这篇文章是一个不错的开始。

The same behavior involving the instance methods is called method overriding. To learn more about method overriding checkout our guide here.

涉及实例方法的相同行为被称为方法覆盖。要了解关于方法覆盖的更多信息,请查看我们的指南

Now, let’s have a look at this practical example:

现在,让我们看一下这个实际的例子。

public class BaseMethodClass {

    public static void printMessage() {
        System.out.println("base static method");
    }
}

BaseMethodClass has a single printMessage() static method.

BaseMethodClass有一个printMessage() static方法。

Next, let’s create a child class with the same signature as in the base class:

接下来,让我们创建一个子类,其签名与基类相同。

public class ChildMethodClass extends BaseMethodClass {

    public static void printMessage() {
        System.out.println("child static method");
    }
}

Here’s how it works:

以下是它的工作原理。

ChildMethodClass.printMessage();

The output after calling the printMessage() method:

调用printMessage()方法后的输出。

child static method

The ChildMethodClass.printMessage() hides the method in BaseMethodClass.

ChildMethodClass.printMessage() BaseMethodClass中隐藏了这个方法。

3.1. Method Hiding vs Overriding

3.1.方法隐藏与重写

Hiding doesn’t work like overriding, because static methods are not polymorphic. Overriding occurs only with instance methods. It supports late binding, so which method will be called is determined at runtime.

隐藏并不像重写那样工作,因为静态方法不是多态的。覆盖只发生在实例方法上。它支持晚期绑定,所以哪个方法将被调用是在运行时决定的。

On the other hand, method hiding works with static ones. Therefore it’s determined at compile time.

另一方面,方法隐藏适用于静态的方法。因此,它是在编译时确定的

4. Conclusion

4.结论

In this article, we went over the concept of method and variable hiding in Java. We showed different scenarios of variable hiding and shadowing. The important highlight of the article is also comparing the method overriding and hiding.

在这篇文章中,我们讨论了Java中方法和变量隐藏的概念。我们展示了变量隐藏和阴影的不同场景。这篇文章的重要亮点还在于比较了方法重写和隐藏。

As usual, the complete code is available over on GitHub.

像往常一样,完整的代码可以在GitHub上找到