阴影的概念 [英] The concept of shadowing

查看:185
本文介绍了阴影的概念的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

给出以下代码:

public class A {
 static final long tooth = 1L;

 static long tooth(long tooth){
  System.out.println(++tooth);
  return ++tooth;
 }

 public static void main(String args[]){
  System.out.println(tooth);
  final long tooth = 2L;
  new A().tooth(tooth);
  System.out.println(tooth);
 }
}

你能解释一下影子的概念吗?
另外,在主方法的代码中实际使用了什么 tooth

Can you please explain me the concept of shadowing ? And another thing, what tooth is actually used in the code from the main method ?

和我知道这是一个非常丑陋的代码,但丑陋的是SCJP书作家的标准选择。

And i know it's a very ugly code, but ugly is the standard choice for SCJP book writers.

推荐答案

有什么魔力阴影作为一个概念。简单地说,对名称的引用将始终引用最近的封闭范围内的实例。在您的示例中:

There's nothing magical about shadowing as a concept. It's simply that a reference to a name will always be referencing the instance within the nearest enclosing scope. In your example:

public class A {
 static final long tooth#1 = 1L;

 static long tooth#2(long tooth#3){
  System.out.println(++tooth#3);
  return ++tooth#3;
 }

 public static void main(String args[]){
  System.out.println(tooth#1);
  final long tooth#4 = 2L;
  new A().tooth#2(tooth#4);
  System.out.println(tooth#4);
}

}

我用一个数字注释每个实例,形式为tooth#N。基本上任何已经在其他地方定义的名称的引入都会超出该范围其余部分的早期定义。

I've annotated each instance with a number, in the form "tooth#N". Basically any introduction of a name that is already defined somewhere else will eclipse the earlier definition for the rest of that scope.

这篇关于阴影的概念的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆