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

2019年2月16日16:57:34 发表评论 983 views
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

发表评论

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: