package example; public class Demo { public A a(final String s){ //这里的参数引用必须是final,否则编译报错 return new A(){ private String name=s; public String getname(){ return name; } }; } public static void main(String[] args) { Demo d=new Demo(); A a=d.a("张三"); System.out.println(a.getname()); } } abstract class A{ private String name; public abstract String getname(); }