|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 2022-04-03 18:06  yanqi_vip  阅读(34)  评论(0)    收藏  举报

导航