今天遇到遮蔽(shadowing)的问题,故翻开JLS看
但是这句话我始终没用读懂,自己的英语实在不咋地,没办法来这问一下
A declarations d of a type named n shadows the declarations of any other types named n that are in the scope
at the point where d occurs throughout the scope of d.
纯英语问题,呵呵,想了好久也没读明白这句话
但是这句话我始终没用读懂,自己的英语实在不咋地,没办法来这问一下
A declarations d of a type named n shadows the declarations of any other types named n that are in the scope
at the point where d occurs throughout the scope of d.
纯英语问题,呵呵,想了好久也没读明白这句话
在点这里d出现整个D.范围
D是一个class1的方法
而int i是D中的声明class1{
int i =0;
public void D(){
int i = 1;
System.out.println(i);
}
}// output: 1也就是说D遮蔽了类class1中的变量i其实LZ已经会了
A declaration d of a type named n是说 声明了类型n的声明语句d;
declarations of any other types named n 是说 其它声明了名为n的类型的声明语句;
A declaration d of a type named n shadows the declarations of any other types named n 合起来就是“声明了类型n的声明语句会遮蔽其它声明了类型n的声明语句”,在哪儿遮蔽呢?看后面的定语从句:
that are in scope at the point where d occurs throughout the scope of d,that就是指那些其它的声明类型n的声明语句,that are in scope 就是说这些被遮蔽的声明是处在一个范围内的,这个范围就是at the point where d occurs throughout the scope of d,既从声明开始直到d作用域结束。
网上关于类型遮蔽的例子比较多,随手搜了一个:
class String{
}
public final class main{
public static void main(String[] args){
}
}
这里的String就会遮蔽java.lang.String,当然这段代码通不过编译的。