The method defines an inner class 12.4

Theoretically the internal class can be defined at any position on the class, or the block of code comprises conventional method, defined in the internal class is relatively common in the conventional method.
Example: in the class definition of the internal methods.

public class Outer264 {
       private String msg="www.hmm.com";   //外部类属性
       public void fun(long time) {       //外部类方法
    	   class Inner{                     //方法中定义内部类
    		   public void println() {      
    			   System.out.println(Outer264.this.msg);//外部类属性
    			   System.out.println(time);              //方法参数
    		   }
    	   }
    	   new Inner().println();                   //方法中直接实例化内部类对象
       }
      
} 

public class Java265 {
       public static void main(String[] args) {
		new Outer264().fun(55555); //调用外部类方法
	}
}

Results of the

www.hmm.com
55555

This procedure is defined in the inner classes Inner Outer264.fun () method, and achieves the outer class attributes and fun in Inner member inner class (parameter access method).

In custom mode before JDK1.8, can be found inside the class after adding the final keyword in method parameters and local variables can be accessed, the reason for such support, as long as to support Lambda expressions.

Published 168 original articles · won praise 9 · views 3280

Guess you like

Origin blog.csdn.net/ll_j_21/article/details/104822606
Recommended