📜  Java中的阴影

📅  最后修改于: 2022-05-13 01:54:36.120000             🧑  作者: Mango

Java中的阴影

内部类是指一个类是另一个类的成员。 Java中基本上有四种类型的内部类。嵌套的内部类可以访问外部类的任何私有实例变量。像任何其他实例变量一样,我们可以拥有访问修饰符 private、protected、public 和 default 修饰符。

Java中的Shadowing是在重叠范围内使用相同名称的变量的做法,其中低级范围内的变量覆盖高级范围内的变量。在这里,高级范围的变量被低级范围的变量所遮蔽。在继续之前,需要对这个关键字有基本的了解。

执行:

在这里,我们将讨论一些示例以更好地理解该概念,因为我们将能够更好地理解它以及代码,稍后将讨论如何生成输出。

示例 1

Java
// Java program to Demonstrates Shadowing in Java
 
// Class 1 and 2
// Outer Class
class Shadowing {
 
    // Custom instance variable or member variable
    String name = "Outer John";
 
    // Nested inner class
    class innerShadowing {
 
        // Instance variable or member variable
        String name = "Inner John";
 
        // Method of this class to
        // print content of instance variable
        public void print()
        {
 
            // Print statements
            System.out.println(name);
            System.out.println(Shadowing.this.name);
        }
    }
}
 
// Class 3
// Main class
class GFG {
 
    // Main driver method
    public static void main(String[] args)
    {
 
        // Accessing an inner class by
        // creating object of outer class inside main()
        Shadowing obj = new Shadowing();
 
        Shadowing.innerShadowing innerObj
            = obj.new innerShadowing();
 
        // Calling method defined inside inner class
        // inside main() method
        innerObj.print();
    }
}


Java
// Java program to demonstrates Shadowing in Java
 
// Class 1 and 2
// Outer Class
class Shadowing {
 
    // Instance variable or member variable
    String name = "Outer John";
 
    // Nested class
    // Inner Class
    class innerShadowing {
 
        // Instance variable or member variable
        String name = "Inner John";
 
        // Method of inner class
        // To print the content
        public void print(String name)
        {
 
            // Print statements
 
            System.out.println(name);
            // This keyword refers to current instance
            // itself
            System.out.println(this.name);
            System.out.println(Shadowing.this.name);
        }
    }
}
 
// Class 3
// Main class
class GFG {
 
    // Main driver method
    public static void main(String[] args)
    {
 
        // Accessing an inner class by
        // creating object of Outer class inside main()
        // method
        Shadowing obj = new Shadowing();
 
        Shadowing.innerShadowing innerObj
            = obj.new innerShadowing();
 
        // Function Call
        innerObj.print("Parameter John");
    }
}


输出
Inner John
Outer John

输出说明:

在此示例中,您可以看到 name 在Shadowing类和innerShadowing类中被声明为 String 变量。当我们只打印名称时,它会打印存储在innerShadowing类中的名称值,因为此类的范围小于外部类,因此它会覆盖名称的值。

让我们看另一个更清楚地阐明概念的示例,如下所示:

示例 2

Java

// Java program to demonstrates Shadowing in Java
 
// Class 1 and 2
// Outer Class
class Shadowing {
 
    // Instance variable or member variable
    String name = "Outer John";
 
    // Nested class
    // Inner Class
    class innerShadowing {
 
        // Instance variable or member variable
        String name = "Inner John";
 
        // Method of inner class
        // To print the content
        public void print(String name)
        {
 
            // Print statements
 
            System.out.println(name);
            // This keyword refers to current instance
            // itself
            System.out.println(this.name);
            System.out.println(Shadowing.this.name);
        }
    }
}
 
// Class 3
// Main class
class GFG {
 
    // Main driver method
    public static void main(String[] args)
    {
 
        // Accessing an inner class by
        // creating object of Outer class inside main()
        // method
        Shadowing obj = new Shadowing();
 
        Shadowing.innerShadowing innerObj
            = obj.new innerShadowing();
 
        // Function Call
        innerObj.print("Parameter John");
    }
}
输出
Parameter John
Inner John
Outer John

输出说明:

在此示例中,我们将参数传递给 print() 方法。所以我们现在可以看到,为了打印我们需要使用'this'的内部类的名称,因为 print() 方法的范围小于内部类的范围,所以它也覆盖了内部类的名称。