|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   yanqi_vip  阅读(5)  评论(0编辑  收藏  举报

相关博文:
阅读排行:
· 无需6万激活码!GitHub神秘组织3小时极速复刻Manus,手把手教你使用OpenManus搭建本
· Manus爆火,是硬核还是营销?
· 终于写完轮子一部分:tcp代理 了,记录一下
· 别再用vector<bool>了!Google高级工程师:这可能是STL最大的设计失误
· 单元测试从入门到精通
< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5

导航

统计

点击右上角即可分享
微信分享提示