java——匿名内部类:get方法

interface A
{
    public void fun1();
}
class B {
    int i = 10;

    public void get(A a)//用于A接口杜希昂的实例化并调用fun1()方法
    {
        a.fun1();
    }
    public void test()
    {
        this.get(new A()//此段实现了A接口中的fun1()方法,并把整个的一个实现类传递到了方法get中()
        {
            public void fun1()
            {
                System.out.println(i);
            }
        }
    );//此段实现了A接口中的fun1()方法,并把整个的一个实现类传递到了方法get中()

}
}




/**
 * Created by xabcd on 2019/2/16.
*/
public class test_niming
{
    public static void main(String[] args)
    {
        B b = new B ();
        b.test();
    }
}


结果:
10

留下评论

您的邮箱地址不会被公开。 必填项已用 * 标注