在java中,什么是Variable-Shadowing和Variable-Hiding ?

java允许我们声明多种类型的变量,不过,虽然类型是繁多的 , 但变量总可以分为一下几种类别 :

  1. 实例变量(Instant Variables) – 是定义在类中的变量(字段), (are defined inside a class and have object level scope);
  2. 类变量(class Variables) – 是在类中用static定义的变量 , 这些变量被所有类的实例对象所共享 ;

(are defined inside a class with static keyword , these variables have a class level scope and are common to all objects of the same class )

3.本地变量(Local Variables) – 是在方法或各种条件块中定义的变量, 用有块级的作用域,并且只能在定义它的块域中被访问;

class Parent {
### //Declaring instance variable by name 'x'
    String x = "Parent's Instance Variable" ; 
    
    public void printInstanceVariable(){ System.out.printn(x); }
    
    public void printLocalVariable(){
       //Shadowing instance variable 'x' by a local variable with same name 
       String x = "Local Variable"; 
       System.out.println(x); 
       
       //if we still want to access instance Variable , we do that by using 'this.x'
       System.out.println(this.x) ;
    }
}
class Child extends Parent {
    // Hiding Parent class's variable 'x' by defining a variable in child class with same name 
    String x = "Child's Instance Variable ";
    
    @Override 
    public void printInstancVariable(){
        System.out.print(x);
        
        //If we still want to access variable from super class ,we do that by using 'super.x'
        System.out.print(","+super.x+"\n");
    }
}


复制代码

#什么是Varable-Shadowing

当我们在一个闭合的内嵌代码块中定义一个变量a ,而外部代码块有一个与之重名的变量时, 便发生了Variable-Shadowing ; 换句话说 ,当Local-Variable与Instance-Variable重名时,Local-Variable便在方法块中Shadow了外部的Instance-Variable; 例如在下列的例子中 ,Instance-Variable x 便被在printLocalVariable()方法中的Local-Variable x shadow了;

class Parent {
### //Declaring instance variable by name 'x'
    String x = "Parent's Instance Variable" ; 
    
    public void printInstanceVariable(){ System.out.printn(x); }
    
    public void printLocalVariable(){
       //Shadowing instance variable 'x' by a local variable with same name 
       String x = "Local Variable"; 
       System.out.println(x); 
       
       //if we still want to access instance Variable , we do that by using 'this.x'
       System.out.println(this.x) ;
    }
}
复制代码

什么是Variable-Hiding

当我们在子类中定义了一个与父类重名的字段(变量)时,便发生了Variable-Hiding,即在子类中声明了一个变量,它的名称与从父类中继承下来的某个变量相同。当发生Variable—Hiding时 , 子类便会Hiding从父类中继承的变量 ;

换句话说,当子类和父类都具有一个同名的变量时, 子类变量便会隐藏父类变量 ;

以下的例子 , 我们在子类child中hiding了从父类中继承的变量x

class Child extends Parent {
    // Hiding Parent class's variable 'x' by defining a variable in child class with same name 
    String x = "Child's Instance Variable ";
    
    @Override 
    public void printInstancVariable(){
        System.out.print(x);
        
        //If we still want to access variable from super class ,we do that by using 'super.x'
        System.out.print(","+super.x+"\n");
    }
}
复制代码

#Variable-Hiding(变量隐藏)和Method-Overriding(方法覆写)并不是一回事儿

Variable-Hiding和Method-Overriding 看起来貌似是一回事儿 ,但实际上它们并不是一回事儿 ,Overriding这种性质是只针对方法而言的 ,同理,Hiding这种性质只针对变量而言 ;

当我们讨论方法覆写(Method-Overriding)时,子类的方法是完全覆盖掉从父类继承来的方法的,即覆写方法已经完全没有父类特征了,完全就是子类自身的;以至于当我们用一个父类型的引用变量去指向一个子类的实例对象时,访问的完全就是子类型的方法;

不过对Variable-Hiding来说 ,子类仅仅只是隐藏了继承来的变量,而不是覆盖掉 。当我们用一个父类型的引用变量去指向一个子类的实例对象时 ,访问的便是从父类中继承下来的字段(变量);

当用父类型的引用变量去指向一个子类的实例对象,且子类的Instance-Variable和父类的Instance-Variable重名,那么用引用变量访问的Instance-Variable将会是父类的

    public class EntertanceClass{
	public static void main(String[] args ) throws Exception {
		
		Parent parent = new Parent();
		parent.printInstanceVariable(); // Output - "Parent's Instance Variable"
		
		System.out.println(parent.x);  //Output - 'Parent's Instance Variable'
		
		Child child = new Child() ;
		child.printInstanceVariable(); // Output - "Child's Instance Variable , Parent's Instance Variable"
		System.out.println(child.x); // Output - "Child's Instance Variable"
		
		parent = child ; // Or parent = new child();
		parent.printInstanceVariable();  // Output - "Child's Instance Variable , Parent's Instance Variable"
		System.out.println(parent.x);	// Output - "Parent's Instance Variable"
		
		// Accessing child 's variable from parent 's reference by type casting 
		System.out.println(((Child)parent).x );  // Output - 'Child's Instance Variable'
		
	}
}

复制代码
© 版权声明
THE END
喜欢就支持一下吧
点赞0 分享