class demo
{
public static void main(String[] args)
{
new outer()
{
public void method()
{
System.out.println("hhhhh");
}
}.method();
} /* 为什么不能在这里定义匿名内部类,
new outer()
{
public void method()
{
System.out.println("ccccccc");
}
}.method(); */
}class outer
{
public void method()
{
System.out.println("qqqqq");
}
}
Java类
{
public static void main(String[] args)
{
new outer()
{
public void method()
{
System.out.println("hhhhh");
}
}.method();
} /* 为什么不能在这里定义匿名内部类,
new outer()
{
public void method()
{
System.out.println("ccccccc");
}
}.method(); */
}class outer
{
public void method()
{
System.out.println("qqqqq");
}
}
Java类
所以只要是new Abc()的地方,都可以是内部类
比如Abc a = new Abc(){...};
new一般写在方法内部,当然也可以用于初始化变量,所以不能说只能在方法体内。
class Person(){
public void do(Per per){
}
};
interface Per(){};
class Test(){
//第一种实现方式
Person p=new Person(){
//这里可以写你想写的方法
};
//第二种实现方式p.do(new Per(){
//写你想写的
});}