2021-04-18 10:57  阅读(94)
文章分类:Java 基础教程 文章标签:JavaJava 教程
©  原文作者:w3cschool 原文地址:https://www.w3cschool.cn/java/java-class-instance.html

Java面向对象设计 - Java内部类成员

内部类可以访问其所有实例成员,实例字段和其封闭类的实例方法。

    class Outer {
      private int value = 2014;
      public class Inner {
        public void printValue() {
          System.out.println("Inner: Value  = " + value);
        }
      } // Inner class ends here
    
      public void printValue() {
        System.out.println("Outer: Value  = " + value);
      }
      public void setValue(int newValue) {
        this.value = newValue;
      }
    }
    
    public class Main {
      public static void main(String[] args) {
        Outer out = new Outer();
        Outer.Inner in = out.new Inner();
    
        out.printValue();
        in.printValue();
    
        out.setValue(2015);
    
        out.printValue();
        in.printValue();
      }
    }
    

上面的代码生成以下结果。

2021041810519_1.png

例子

以下代码显示如何访问内部类的内部变量。

    public class Main {
      public static void main(String[] args) {
        Outer out = new Outer();
        Outer.Inner in = out.new Inner();
    
        out.printValue();
        in.printValue();
    
        out.setValue(3);
    
        out.printValue();
        in.printValue();
      }
    }
    class Outer {
      private int value = 1;
      public class Inner {
        private int value = 2;
        public void printValue() {
          System.out.println("Inner: Value  = " + value);
        }
      } // Inner class ends here
      public void printValue() {
        System.out.println("Outer: Value  = " + value);
      }
      public void setValue(int newValue) {
        this.value = newValue;
      }
    }
    

上面的代码生成以下结果。

2021041810511_2.png

在内部类中使用关键字this

以下代码显示如何在内部类中使用关键字this。

    class Outer {
      private int value = 1;
      class QualifiedThis {
        private int value = 2;
    
        public void printValue() {
          System.out.println("value=" + value);
          System.out.println("this.value=" + this.value);
          System.out.println("QualifiedThis.this.value=" + QualifiedThis.this.value);
        }
    
        public void printHiddenValue() {
          int value = 2;
          System.out.println("value=" + value);
          System.out.println("this.value=" + this.value);
    
          System.out.println("QualifiedThis.this.value=" + QualifiedThis.this.value);
        }
      }
      public void printValue() {
        System.out.println("value=" + value);
        System.out.println("this.value=" + this.value);
      }  
    }
    
    public class Main {
      public static void main(String[] args) {
        Outer outer = new Outer();
        Outer.QualifiedThis qt = outer.new QualifiedThis();
        System.out.println("printValue():");
        qt.printValue();
    
        System.out.println("printHiddenValue():");
        qt.printHiddenValue();
        outer.printValue();
      }
    }
    

上面的代码生成以下结果。

202104181059_3.png

隐藏变量

如果实例变量名称被隐藏,您必须使用关键字this或类名称以及关键字this限定其名称。

    class TopLevelOuter {
      private int v1 = 100;
    
      // Here, only v1 is in scope
      public class InnerLevelOne {
        private int v2 = 200;
    
        // Here, only v1 and v2 are in scope
        public class InnerLevelTwo {
          private int v3 = 300;
    
          // Here, only v1, v2, and v3 are in scope
          public class InnerLevelThree {
            private int v4 = 400;
    
            // Here, all v1, v2, v3, and v4 are in scope
    
          }
        }
      }
    }
    

从外部类引用变量

以下代码显示如何从外部类引用变量。

    public class Test{
      private int value = 1;
      public class Inner {
        private int value = 2;
    
        public void printValue() {
          System.out.println("Inner: Value  = " + value);
          System.out.println("Outer: Value  = " + Test.this.value);
        }
      } // Inner class ends here
    
      public void printValue() {
        System.out.println("\nOuter - printValue()...");
        System.out.println("Outer: Value  = " + value);
      }
    
      public void setValue(int newValue) {
        System.out.println("\nSetting  Outer"s value to " + newValue);
        this.value = newValue;
      }
    
    }
    
点赞(0)
版权归原创作者所有,任何形式转载请联系作者; Java 技术驿站 >> Java 内部类成员
上一篇
Java 内部类对象
下一篇
Java 继承