• 局部内部类的特点与使用


    特点

    1. 不能用public private static 修饰类
    2. 访问方法内的变量必须是finnal类型
    3. 在方法体内返回实例
    4. 外部类必然无法实例局部类

    使用

    
    import lombok.extern.java.Log;
    import lombok.extern.slf4j.Slf4j;
    @Slf4j
    public class LocalInnerClassTest {
        private String outpara1="out param 1";
        private static String outpara2="out param 2";
        
        interface Inner{
            void print();
        }
    
        public Inner testLocalClass(){
            final String localInput="localInput";
           class LocalClass implements Inner{
                    private String innerpara1="out param 1";
                    
                    private final   String innerpara2="out param 2";
    
                @Override
                public void print() {
                  log.info("begin print"); 
                  log.info("print {}",localInput);
                  log.info("out param1 :{}",outpara1); 
                }
    
            }
            return new LocalClass();
        }
        public static Inner testLocalClass2(){
            class LocalClass implements Inner{
                     private  String innerpara1="iner param 1";
                     
                     private   String innerpara2="iner param 2";
     
                 @Override
                 public void print() {
                   log.info("begin print"); 
                   log.info("inner param1 :{}",innerpara1); 
                   log.info("inner param2  :{}",innerpara2); 
                 }
     
             }
             return new LocalClass();
         }
        public static void main(String[] args) {
            LocalInnerClassTest test=new LocalInnerClassTest();
            test.testLocalClass().print();
            testLocalClass2().print();
        }
    }
    
  • 相关阅读:
    OCP-1Z0-053-200题-125题-155
    OCP-1Z0-053-200题-127题-154答案貌似都不对?
    OCP-1Z0-053-200题-128题-281
    OCP-1Z0-053-200题-129题-153
    OCP-1Z0-053-200题-130题-288
    OCP-1Z0-053-200题-131题-152
    OCP-1Z0-053-200题-132题-272
    OCP-1Z0-053-200题-133题-151
    OCP-1Z0-053-200题-134题-4
    OCP-1Z0-053-200题-135题-150
  • 原文地址:https://www.cnblogs.com/JuncaiF/p/11153340.html
Copyright © 2020-2023  润新知