特点
- 不能用public private static 修饰类
- 访问方法内的变量必须是finnal类型
- 在方法体内返回实例
- 外部类必然无法实例局部类
使用
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();
}
}