|NO.Z.00074|——————————|BigDataEnd|——|Java&特殊类.V02|——|Java.v02|普通内部类.v01|定义|

一、普通内部类的定义
### --- 普通(成员)内部类的格式

~~~     ——>        访问修饰符class 外部类的类名{
~~~     ——>            访问修饰符class 内部类的类名{
~~~     ——>                内部类的类体;
~~~     ——>            }
~~~     ——>        }
二、编程代码
package com.yanqi.task10;

/**
 * 编程实现普通内部类的定义和使用       -  文档注释
 */
public class NormalOuter {
    private int cnt = 1;

    // 定义普通内部类,隶属于外部类的成员,并且是对象层级
    /*private*/public /*final*/ class NormalInner {
        private int ia = 2;
        private int cnt = 3;
        public NormalInner() {
            System.out.println("普通内部类的构造方法体执行到了!");
        }

        public void show() {
            System.out.println("外部类中变量cnt的数值为:" + cnt); // 1
            System.out.println("ia = " + ia); // 2
        }

        public void show2(int cnt) {
            System.out.println("形参变量cnt = " + cnt);  // 局部优先原则  4
            System.out.println("内部类中cnt = " + this.cnt); // 3
            System.out.println("外部类中cnt = " + NormalOuter.this.cnt); // 1
        }
    }
}
三、编程代码
package com.yanqi.task10;

public class NormalOuterTest {

    public static void main(String[] args) {

        // 1.声明NormalOuter类型的引用指向该类型的对象
        NormalOuter no = new NormalOuter();
        // 2.声明NormalOuter类中内部类的引用指向内部类的对象
        NormalOuter.NormalInner ni = no.new NormalInner();
        // 调用内部类中的show方法
        ni.show();

        System.out.println("---------------------------------------------");
        ni.show2(4);
    }
}
四、编译打印
D:\JAVA\jdk-11.0.2\bin\java.exe "-javaagent:D:\IntelliJIDEA\IntelliJ IDEA 2019.3.3\lib\idea_rt.jar=54045:D:\IntelliJIDEA\IntelliJ IDEA 2019.3.3\bin" -Dfile.encoding=UTF-8 -classpath E:\NO.Z.10000——javaproject\NO.H.00001.javase\javase\out\production\javase com.yanqi.task10.NormalOuterTest
普通内部类的构造方法体执行到了!
外部类中变量cnt的数值为:3
ia = 2
---------------------------------------------
形参变量cnt = 4
内部类中cnt = 3
外部类中cnt = 1

Process finished with exit code 0

 
 
 
 
 
 
 
 
 

Walter Savage Landor:strove with none,for none was worth my strife.Nature I loved and, next to Nature, Art:I warm'd both hands before the fire of life.It sinks, and I am ready to depart
                                                                                                                                                   ——W.S.Landor

 

 

posted on 2022-04-03 18:04  yanqi_vip  阅读(11)  评论(0)    收藏  举报

导航