|NO.Z.00078|——————————|BigDataEnd|——|Java&特殊类.V06|——|Java.v06|局部内部类.v01|定义|

一、局部内部类的蒂尼
### --- 局部(方法)内部类的格式

~~~     ——>        访问修饰符class 外部类的类名{
~~~     ——>            访问修饰符返回值类型成员方法名(形参列表){
~~~     ——>                class 内部类的类名{
~~~     ——>                    内部类的类体;
~~~     ——>                }
~~~     ——>            }
~~~     ——>        }
二、编程代码
package com.yanqi.task10;

/**
 * 编程实现局部内部类的定义和使用
 */
public class AreaOuter {
    private int cnt = 1;

    public void show() {

        // 定义一个局部变量进行测试,从Java8开始默认理解为final关键字修饰的变量
        // 虽然可以省略final关键字,但建议还是加上
        final int ic = 4;

        // 定义局部内部类,只在当前方法体的内部好使    拷贝一份
        class AreaInner {
            private int ia = 2;

            public AreaInner() {
                System.out.println("局部内部类的构造方法!");
            }

            public void test() {
                int ib = 3;
                System.out.println("ia = " + ia); // 2
                System.out.println("cnt = " + cnt); // 1
                //ic = 5;  Error
                System.out.println("ic = " + ic); // 4
            }
        }

        // 声明局部内部类的引用指向局部内部类的对象
        AreaInner ai = new AreaInner();
        ai.test();
    }

}
三、编程代码
package com.yanqi.task10;

public class AreaOuterTest {

    public static void main(String[] args) {

        // 1.声明外部类类型的引用指向外部类的对象
        AreaOuter ao = new AreaOuter();
        // 2.通过show方法的调用实现局部内容类的定义和使用
        ao.show();
    }
}
四、编译打印
D:\JAVA\jdk-11.0.2\bin\java.exe "-javaagent:D:\IntelliJIDEA\IntelliJ IDEA 2019.3.3\lib\idea_rt.jar=54183: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.AreaOuterTest
局部内部类的构造方法!
ia = 2
cnt = 1
ic = 4

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

相关博文:
阅读排行:
· 全程不用写代码,我用AI程序员写了一个飞机大战
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 记一次.NET内存居高不下排查解决与启示
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
· DeepSeek 开源周回顾「GitHub 热点速览」
< 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

导航

统计

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